A function which resolves a promise whether a given predicate becomes true.
npm i wait-for-predicate
const { waitForPredicate, TIMEOUT_EXPIRED } = require("wait-for-predicate");
let exit = false;
const predicate = () => !!exit;
try {
setTimeout(() => { exit = true }, 5000);
await waitForPredicate(predicate, { timeout: 3000 });
} catch(error) {
if (error.message === TIMEOUT_EXPIRED) {
console.error(TIMEOUT_EXPIRED);
}
throw error;
}