Skip to content

Instantly share code, notes, and snippets.

@tabiodun
Created March 20, 2020 16:00
Show Gist options
  • Save tabiodun/b865265731640ab1a3f82afc6e5e63b3 to your computer and use it in GitHub Desktop.
Save tabiodun/b865265731640ab1a3f82afc6e5e63b3 to your computer and use it in GitHub Desktop.
Polling in Javascript
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