Last active
July 11, 2019 14:38
-
-
Save w35l3y/d7a40c795c04799ba5a77917d4e5ffce to your computer and use it in GitHub Desktop.
Verifica periodicamente o resultado de uma promessa
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
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