Last active
September 8, 2016 11:04
-
-
Save naholyr/31d575f40c93472264731ec9139b5aa4 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