pooledMap
import { pooledMap } from "jsr:@ende/async/unstable-pool";function · experimental
function pooledMap<T, R>(array: Iterable<T> | AsyncIterable<T>, iteratorFn: (data: T) => Promise<R>, poolLimit: number): AsyncIterableIterator<R>Transforms values from an (async) iterable into another async iterable.
The transforms are done concurrently, with a max concurrency defined by
poolLimit.
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.
poolLimit: numberThe maximum count of items being processed concurrently.
Returns
The async iterator with the transformed values.
Example: Usage
import { pooledMap } from "@ende/async/unstable-pool";
import { assertEquals } from "@ende/assert";
const results = pooledMap(
[1, 2, 3],
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
2,
);
assertEquals(await Array.fromAsync(results), [1, 2, 3]);
function · experimental
function pooledMap<T, R>(array: Iterable<T> | AsyncIterable<T>, iteratorFn: (data: T) => Promise<R>, options: PooledMapOptions): AsyncIterableIterator<R>Transforms values from an (async) iterable into another async iterable.
The transforms are done concurrently, with a max concurrency defined by
PooledMapOptions.poolLimit.
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.
options: PooledMapOptionsConfiguration for concurrency and cancellation.
Returns
The async iterator with the transformed values.
Example: Usage with options
import { pooledMap } from "@ende/async/unstable-pool";
import { assertEquals } from "@ende/assert";
const results = pooledMap(
[1, 2, 3],
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
{ poolLimit: 2 },
);
assertEquals(await Array.fromAsync(results), [1, 2, 3]);
Example: Cancellation with AbortSignal
import { pooledMap } from "@ende/async/unstable-pool";
import { assertRejects } from "@ende/assert";
const controller = new AbortController();
const results = pooledMap(
[1, 2, 3, 4, 5],
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
{ poolLimit: 2, signal: controller.signal },
);
controller.abort(new Error("cancelled"));
await assertRejects(
() => Array.fromAsync(results),
Error,
"cancelled",
);
function · experimental
function pooledMap<T, R>(array: Iterable<T> | AsyncIterable<T>, iteratorFn: (data: T) => Promise<R>, options: number | PooledMapOptions): AsyncIterableIterator<R>