Skip to content

Instantly share code, notes, and snippets.

@gr0uch
Last active September 7, 2015 18:21
Show Gist options
  • Save gr0uch/d9e6b0af3ceee9ddb9dd to your computer and use it in GitHub Desktop.
Save gr0uch/d9e6b0af3ceee9ddb9dd to your computer and use it in GitHub Desktop.
/**
* 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