Last active
August 29, 2015 14:12
-
-
Save MiguelCastillo/ff0a42b3e19e43b60247 to your computer and use it in GitHub Desktop.
Cancelable Promise Sequence
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 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