Skip to content

Instantly share code, notes, and snippets.

@wpm
Last active November 14, 2019 09:59
Show Gist options
  • Save wpm/f745ea6478507c6eb72f to your computer and use it in GitHub Desktop.
Save wpm/f745ea6478507c6eb72f to your computer and use it in GitHub Desktop.
Javascript Polling with Promises
var Promise = require('bluebird');
/**
* Periodically poll a signal function until either it returns true or a timeout is reached.
*
* @param signal function that returns true when the polled operation is complete
* @param interval time interval between polls in milliseconds
* @param timeout period of time before giving up on polling
* @returns true if the signal function returned true, false if the operation timed out
*/
function poll(signal, interval, timeout) {
function pollRecursive() {
return signal() ? Promise.resolve(true) : Promise.delay(interval).then(pollRecursive);
}
return pollRecursive()
.cancellable()
.timeout(timeout)
.catch(Promise.TimeoutError, Promise.CancellationError, function () {
return false;
});
}
/**
* Every 2 seconds, call a signal function that returns true 25% of the time. Timeout after 10 seconds.
*/
poll(randomSignal, 2000, 10000).then(console.log);
function randomSignal() {
var r = Math.random() <= 0.25;
console.log(r ? "Complete" : "Not complete");
return r;
}
@ujwal-setlur
Copy link

@formula1, thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment