Last active
July 25, 2018 04:42
-
-
Save vpusher/ad44644c58ab41d6db71cd69c26ffae6 to your computer and use it in GitHub Desktop.
Promise API extension to resolve them sequentially.
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
// Promise API extension to be able to resolve them sequentially. | |
Promise.serial = function (promises) { | |
return promises.reduce((promiseChain, currentPromise) => { | |
return promiseChain.then(chainResults => | |
// Using Promise.resolve() to be able to take any kind of 'promises' parameter: Promise, Function, Object, ... | |
Promise.resolve().then(currentPromise).then(currentResult => | |
[ ...chainResults, currentResult ] | |
) | |
); | |
}, Promise.resolve([])) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment