-
-
Save davidbgk/b9bee82e9297db7a3c2fff64bcc44cde to your computer and use it in GitHub Desktop.
Run promise-based async API in series or concurrency
This file contains 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
// ( 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