Last active
September 7, 2015 18:21
-
-
Save gr0uch/d9e6b0af3ceee9ddb9dd to your computer and use it in GitHub Desktop.
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
/** | |
* Take an iterable of Promises and invoke them serially, otherwise identical | |
* to the `Promise.all` static method. | |
* | |
* @param {Promise[]} promises | |
* @return {Promise} | |
*/ | |
function serial (promises) { | |
const results = [] | |
return (Array.isArray(promises) ? | |
promises : Array.from(promises)) | |
.reduce((chain, promise, index) => | |
chain.then(result => { | |
if (index) results.push(result) | |
return promise | |
}), Promise.resolve()) | |
.then(result => { | |
if (promises.length) results.push(result) | |
return results | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment