Created
March 20, 2020 16:00
-
-
Save tabiodun/b865265731640ab1a3f82afc6e5e63b3 to your computer and use it in GitHub Desktop.
Polling in Javascript
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 poll(fn, fnCondition, ms, attemptsLimit) { | |
let attempts = 0; | |
let result = await fn(); | |
while (fnCondition(result) && attemps < attemptsLimit) { | |
await wait(ms); | |
result = await fn(); | |
attempts++; | |
} | |
return result; | |
} | |
function wait(ms = 1000) { | |
return new Promise(resolve => { | |
console.log(`waiting ${ms} ms...`); | |
setTimeout(resolve, ms); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment