Skip to content

Instantly share code, notes, and snippets.

@vpusher
Last active July 25, 2018 04:42
Show Gist options
  • Save vpusher/ad44644c58ab41d6db71cd69c26ffae6 to your computer and use it in GitHub Desktop.
Save vpusher/ad44644c58ab41d6db71cd69c26ffae6 to your computer and use it in GitHub Desktop.
Promise API extension to resolve them sequentially.
// 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