Skip to content

Instantly share code, notes, and snippets.

@alexanderjamesking
Last active September 1, 2016 07:20
Show Gist options
  • Save alexanderjamesking/8f299ed8a6c351ac873c7a6d9573ec65 to your computer and use it in GitHub Desktop.
Save alexanderjamesking/8f299ed8a6c351ac873c7a6d9573ec65 to your computer and use it in GitHub Desktop.
/**
* repeats a check until the check is true or the timeout is reached
* returns a promise that we can return to mocha
*/
function promisePoller(condition,
interval = 10,
timeout = 1000,
onTimeout = new Error("Timed out")) {
return new Promise(function (resolve, reject) {
function check() {
setTimeout(() => {
if (condition()) resolve()
else check()
}, interval)
}
setTimeout(() => { reject(onTimeout) }, timeout)
check()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment