Last active
November 14, 2019 09:59
-
-
Save wpm/f745ea6478507c6eb72f to your computer and use it in GitHub Desktop.
Javascript Polling with Promises
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
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; | |
} |
module.exports.pollUntil = function(fn, delay){
var notCanceled = true;
function pollRecursive(){
return Promise.resolve().then(fn).then((boo)=>(
notCanceled && boo && new Promise((res)=>(setTimeout(res, delay)))
.then(pollRecursive)
));
}
var p = pollRecursive();
p.cancel = ()=>(
notCanceled = false
);
};
@formula1, thanks for this!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jopek, I ran into the same issue! What is the preferred way of making a polling function with promises now?