I am struggling to make a simple waiting function in my program. I want to use promises and async await if possible. What I have so far:
function waitForCondition(conditionObj) {
var start_time = new Date().getTime()
function checkFlag() {
if (conditionObj.arg == conditionObj.test) {
console.log('met');
return new Promise(resolve => setTimeout(resolve, 1));
} else if (new Date() > start_time + 3000) {
console.log('not met, time out');
return new Promise(resolve => setTimeout(resolve, 1));
} else {
window.setTimeout(checkFlag, 1000);
}
}
checkFlag();
}
async function run() {
console.log('before');
await waitForCondition({arg: '1', test: '1'})
console.log('after');
}
run();
It should check every 1 second for a maximum time of 3 seconds. The console should look like this:
'before'
'met'
'after'
I am struggling to make a simple waiting function in my program. I want to use promises and async await if possible. What I have so far:
function waitForCondition(conditionObj) {
var start_time = new Date().getTime()
function checkFlag() {
if (conditionObj.arg == conditionObj.test) {
console.log('met');
return new Promise(resolve => setTimeout(resolve, 1));
} else if (new Date() > start_time + 3000) {
console.log('not met, time out');
return new Promise(resolve => setTimeout(resolve, 1));
} else {
window.setTimeout(checkFlag, 1000);
}
}
checkFlag();
}
async function run() {
console.log('before');
await waitForCondition({arg: '1', test: '1'})
console.log('after');
}
run();
It should check every 1 second for a maximum time of 3 seconds. The console should look like this:
'before'
'met'
'after'
Share
Improve this question
edited Sep 10, 2021 at 12:45
Hanna Bilous
3283 silver badges17 bronze badges
asked Aug 3, 2017 at 15:52
Ian WIan W
9904 gold badges12 silver badges30 bronze badges
3 Answers
Reset to default 13You have to return
a promise:
function waitForCondition(conditionObj) {
return new Promise(resolve => {
var start_time = Date.now();
function checkFlag() {
if (conditionObj.arg == conditionObj.test) {
console.log('met');
resolve();
} else if (Date.now() > start_time + 3000) {
console.log('not met, time out');
resolve();
} else {
window.setTimeout(checkFlag, 1000);
}
}
checkFlag();
});
}
async function run() {
console.log('before');
await waitForCondition({arg: '1', test: '1'})
console.log('after');
}
run();
I refactored your code a bit. To get the current time, use Date.now()
. And you should be OK with calling resolve
without a timeout of 1 millisecond.
I believe what you are really looking for is
function waitForCondition(conditionObj) {
var start_time = new Date().getTime()
async function checkFlag() {
if (conditionObj.arg == conditionObj.test) {
console.log('met');
// return something?
} else if (new Date() > start_time + 3000) {
console.log('not met, time out');
// throw some error?
} else {
await new Promise(resolve => setTimeout(resolve, 1000));
return checkFlag();
}
}
return checkFlag();
}
or with a loop instead of the recursion
async function waitForCondition(conditionObj) {
var start_time = new Date().getTime()
while (true) {
if (conditionObj.arg == conditionObj.test) {
console.log('met');
break; // or return
}
if (new Date() > start_time + 3000) {
console.log('not met, time out');
break; // or throw
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
Your function waitForCondition
implicitely returns undefined
. try to add a return statment at the end of it:
return checkFlag();
And as mentionned in comment, there one condition where checkFlag()
returns undefined
that you might want to handle by returning a Promise
.