Skip to content

Instantly share code, notes, and snippets.

@michaelhelvey
Created September 15, 2016 23:55
Show Gist options
  • Save michaelhelvey/0f503cbb7c02cd4207ba4cc0875b2f5b to your computer and use it in GitHub Desktop.
Save michaelhelvey/0f503cbb7c02cd4207ba4cc0875b2f5b to your computer and use it in GitHub Desktop.
// Callback must returns a promise.
// This function, unlike `Promise.all`, will resolve
// one promise at a time, in the original array order
const serial = (arr, callback) => {
return new Promise((resolve, reject) => {
arr.reduce((previous, current, index) => {
return previous.then(() => callback(current)).then(() => {
if (index === (arr.length - 1)) resolve();
}).catch(e => reject(e));
}, Promise.resolve());
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment