Unexpected behaviour with `async` functions
Opened this issue · 0 comments
LukeSavefrogs commented
When the testCondition
callback is an async
function, the waitUntil
method returns immediately, without waiting at all.
The following fixes the problem by using an async function by default (needs to be tested):
const waitUntil = function (testCondition, timeout_ms = 30000, checkInterval_ms = 1000) {
let start_ts = new Date().getTime();
return new Promise((resolve, reject) => {
let timer = setInterval(async () => {
let elapsed_time: number = parseInt((new Date().getTime() - start_ts).toString());
// Se il timeout è un numero
if (
!!timeout_ms &&
!isNaN(timeout_ms) &&
timeout_ms > 0 &&
elapsed_time >= timeout_ms
) {
clearInterval(timer);
reject(
new Error(`Timeout of ${timeout_ms} ms exceeded (${elapsed_time} real)`)
);
}
if (await testCondition()) {
clearInterval(timer);
resolve({
msg:
`The specified condition was met before the ${timeout_ms} ms timeout`,
time: elapsed_time,
given_timeout: timeout_ms,
});
}
}, checkInterval_ms);
});
}