pooledMap
import { pooledMap } from "jsr:@ende/async";function
function pooledMap<T, R>(poolLimit: number, array: Iterable<T> | AsyncIterable<T>, iteratorFn: (data: T) => Promise<R>): AsyncIterableIterator<R>pooledMap transforms values from an (async) iterable into another async iterable. The transforms are done concurrently, with a max concurrency defined by the poolLimit.
If an error is thrown from iterableFn, no new transformations will begin.
All currently executing transformations are allowed to finish and still
yielded on success. After that, the rejections among them are gathered and
thrown by the iterator in an AggregateError.
poolLimit: numberThe maximum count of items being processed concurrently. Must be a positive integer.
array: Iterable<T> | AsyncIterable<T>The input array for mapping.
iteratorFn: (data: T) => Promise<R>The function to call for every item of the array.
Returns
The async iterator with the transformed values.
Example: Usage
import { pooledMap } from "@ende/async/pool";
import { assertEquals } from "@ende/assert";
const results = pooledMap(
2,
[1, 2, 3],
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
);
assertEquals(await Array.fromAsync(results), [1, 2, 3]);