Skip to content

Instantly share code, notes, and snippets.

@qborreda
Forked from joelnet/promiseSerial.js
Created January 27, 2017 10:14
Show Gist options
  • Save qborreda/6afe52898572cb5af9478c20dd63d6d1 to your computer and use it in GitHub Desktop.
Save qborreda/6afe52898572cb5af9478c20dd63d6d1 to your computer and use it in GitHub Desktop.
/*
* 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