Skip to content

Instantly share code, notes, and snippets.

@kirit0s
Created May 20, 2022 09:22
Show Gist options
  • Save kirit0s/74f16c4cabb68d89f45a293a36983388 to your computer and use it in GitHub Desktop.
Save kirit0s/74f16c4cabb68d89f45a293a36983388 to your computer and use it in GitHub Desktop.
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