mapAsync
The async counterpart to Array.prototype.map: applies fn to every item and resolves to
an array of the results, in input order — regardless of which call finishes first.
Array.prototype.map(asyncFn) alone doesn’t do this: it returns an array of unresolved
promises (Promise<R>[]), not Promise<R[]> — you’d still need to wrap it in
Promise.all(...) yourself, and there’d be no way to cap concurrency. This does both.
With no concurrency given, every call starts immediately (like
Promise.all(array.map(fn))). Pass concurrency to cap how many run at once — e.g. to
avoid overwhelming an API. Rejects with the first error thrown by fn, same as
Promise.all; other already-started calls keep running in the background but their
outcome is ignored.
Available since v3.0.4
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | The array to map over |
fn | function | Async (or sync) function called with `(item, index)` |
concurrency | number | Maximum number of concurrent calls. Defaults to unlimited. (optional) |
Returns
Section titled “Returns”Promise<R[]> — The mapped results, in input order
Examples
Section titled “Examples”Map over an array with an async function
Section titled “Map over an array with an async function”Every call starts immediately, results come back in input order.
Cap concurrency to avoid overwhelming an API
Section titled “Cap concurrency to avoid overwhelming an API”At most concurrency calls run at once; the rest queue and wait.
