Skip to content

Instantly share code, notes, and snippets.

@w35l3y
Last active July 11, 2019 14:38
Show Gist options
  • Save w35l3y/d7a40c795c04799ba5a77917d4e5ffce to your computer and use it in GitHub Desktop.
Save w35l3y/d7a40c795c04799ba5a77917d4e5ffce to your computer and use it in GitHub Desktop.
Verifica periodicamente o resultado de uma promessa
let check = (cb, timeout = 30000, step = 5000) => {
if (0 >= timeout) {
return Promise.reject("TIMEOUT");
}
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Checking...", timeout);
cb().then(found => {
console.log("Checked!", found);
if (found) {
return resolve();
}
return check(cb, timeout - step, step).then(resolve, reject);
}, reject);
}, step);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment