Skip to content

Instantly share code, notes, and snippets.

@MiguelCastillo
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiguelCastillo/ff0a42b3e19e43b60247 to your computer and use it in GitHub Desktop.
Save MiguelCastillo/ff0a42b3e19e43b60247 to your computer and use it in GitHub Desktop.
Cancelable Promise Sequence
var promises = [promise1, promise2, promise3];
// Now you can actually call a cancel interface without affecting the state of the promises.
var cancelable = cancelableSenquence(promises);
function cancelableSequence(promises) {
var cancelled = false;
var cancelable = promises.reduce(function(prev, curr) {
return prev.then(function() {
if (!cancelled) {
return curr;
}
}, function(err) {
cancelled = true;
return err;
})
}, Promise.resolve());
cancelable.cancel = function() {
cancelled = true;
};
return cancelable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment