-
-
Save qborreda/6afe52898572cb5af9478c20dd63d6d1 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) | |
.catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment