Skip to content

Instantly share code, notes, and snippets.

@naholyr
Last active September 8, 2016 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save naholyr/31d575f40c93472264731ec9139b5aa4 to your computer and use it in GitHub Desktop.
Save naholyr/31d575f40c93472264731ec9139b5aa4 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