asyncMap<T, U>
Maps over an array asynchronously, applying the provided callback function to each element.
Without options, every callback starts at once. With a concurrency, at most that many callbacks run at a time, each new one starting as soon as a running one settles, in array order.
With options, the first callback that throws stops new callbacks from starting, and the returned promise rejects with that error at once. Aborting the abortSignal does the same with the signal’s reason. Callbacks already running are not interrupted: pass the same signal into them if they should stop too.
Bounded concurrency adapts the worker pool of radashi’s parallel (MIT, see THIRD-PARTY-NOTICES.md).
Import:
import { asyncMap } from '@mnaoumov/typescript-lib/async';Signature:
function asyncMap(array: T[], callback: (value: T, index: number, array: T[]) => Promisable<U>, options: AsyncMapOptions | undefined): Promise<U[]>Parameters:
| Parameter | Type | Description |
|---|---|---|
array |
T[] | The array to map over. |
callback |
(value: T, index: number, array: T[]) => Promisable<U> | The callback function to apply to each element. |
options |
AsyncMapOptions | undefined | The concurrency limit and abort signal. |
Returns: Promise<U[]> — A Promise that resolves with an array of the results of the callback function, in array order.
Links to this page: