-
-
Save joelnet/b286721a933e7e410120a8a2866dbcc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* promiseSerial resolves Promises sequentially. | |
* @example | |
* const urls = ['/url1', '/url2', '/url3'] | |
* const funcs = urls.map(url => () => $.ajax(url)) | |
* | |
* promiseSerial(funcs) | |
* .then(console.log) | |
* .catch(console.error) | |
*/ | |
const promiseSerial = funcs => | |
funcs.reduce((promise, func) => | |
promise.then(result => func().then(Array.prototype.concat.bind(result))), | |
Promise.resolve([])) | |
// some url's to resolve | |
const urls = ['/url1', '/url2', '/url3'] | |
// convert each url to a function that returns a promise | |
const funcs = urls.map(url => () => $.ajax(url)) | |
// execute Promises in serial | |
promiseSerial(funcs) | |
.then(console.log.bind(console)) | |
.catch(console.error.bind(console)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops. Updated! Thanks!