Created
September 15, 2016 23:55
-
-
Save michaelhelvey/0f503cbb7c02cd4207ba4cc0875b2f5b 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
// 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