Created
May 20, 2022 09:22
-
-
Save kirit0s/74f16c4cabb68d89f45a293a36983388 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { pipe } from 'fp-ts/lib/function'; | |
import type { Separated } from 'fp-ts/lib/Separated'; | |
import * as S from 'fp-ts/lib/Separated'; | |
import type { Task } from 'fp-ts/lib/Task'; | |
import type { TaskEither } from 'fp-ts/lib/TaskEither'; | |
import * as TE from 'fp-ts/lib/TaskEither'; | |
export const traversePar = | |
<T, E, A>(poolLimit: number, task: (t: T) => TaskEither<E, A>) => | |
(dataList: T[]): Task<Separated<E[], A[]>> => | |
async () => { | |
const okList: A[] = []; | |
const errList: E[] = []; | |
const iterator = dataList.values(); | |
const workers = Array.from({ length: Math.max(1, poolLimit) }, async () => { | |
for (const data of iterator) { | |
await pipe( | |
task(data), | |
TE.match( | |
(err) => { | |
errList.push(err); | |
}, | |
(ok) => { | |
okList.push(ok); | |
}, | |
), | |
)(); | |
} | |
}); | |
await Promise.all(workers); | |
return S.separated(errList, okList); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment