12 lines
373 B
TypeScript
12 lines
373 B
TypeScript
type MapFunction<In, Out> = (input: In) => Promise<[In, Out] | null>;
|
|
|
|
export async function map_parallel<In, Out>(
|
|
inputs: Iterable<In>,
|
|
map_fn: MapFunction<In, Out>,
|
|
): Promise<Map<In, Out>> {
|
|
const queue = [...inputs].map((input) => map_fn(input));
|
|
|
|
return new Map<In, Out>(
|
|
(await Promise.all(queue)).filter((job): job is [In, Out] => job !== null),
|
|
);
|
|
}
|