Skip to content

Instantly share code, notes, and snippets.

@davidbgk
Forked from naholyr/promise-all.js
Created September 8, 2016 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidbgk/b9bee82e9297db7a3c2fff64bcc44cde to your computer and use it in GitHub Desktop.
Save davidbgk/b9bee82e9297db7a3c2fff64bcc44cde to your computer and use it in GitHub Desktop.
Run promise-based async API in series or concurrency
// ( T1 => Promise<T2> ) => Array<T1> => Promise<Array<T2>>
export const concurrent = foo => vals =>
Promise.all(vals.map(foo))
export const series = foo => vals =>
vals.reduce((p, val) => p.then(rs => foo(val).then(r => rs.concat([val]))), Promise.resolve([]))
// Usage: promiseOfValues.then(promiseAll.concurrent(functionReturningAPromiseFromAValue))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment