Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.