Created
June 7, 2019 17:42
-
-
Save Xotabu4/9f42147f3aef100a4382d155bd2a7dc8 to your computer and use it in GitHub Desktop.
wait for everything, without browser.wait or driver.wait
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function waitFor(condition, timeout, poolInterval) { | |
const timeoutDeadline = new Date().getTime() + timeout; | |
let iterationRes; | |
do { | |
console.log('#', new Date().getTime(), timeoutDeadline); | |
if (new Date().getTime() > timeoutDeadline) { | |
let timeoutError = new Error("Wait timeout error!"); | |
console.log(condition.toString()) | |
throw timeoutError; | |
} | |
try { | |
iterationRes = await condition(); | |
console.log(interationError); | |
} catch (interationError) { | |
lastErr = interationError; | |
iterationRes = false; | |
} | |
if (!iterationRes) { | |
await new Promise(r => setTimeout(r, poolInterval)); | |
} | |
} while (!iterationRes); | |
} | |
waitFor( | |
async function () { | |
return Promise.resolve(true) | |
}, | |
2000, | |
1000 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment