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'
You 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));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With