Rush StackShopBlogEvents
Skip to main content

Home > @rushstack/node-core-library > Async > mapAsync

Async.mapAsync() method

Given an input array and a callback function, invoke the callback to start a promise for each element in the array. Returns an array containing the results.

Signature:

static mapAsync<TEntry extends IWeighted, TRetVal>(iterable: Iterable<TEntry> | AsyncIterable<TEntry>, callback: (entry: TEntry, arrayIndex: number) => Promise<TRetVal>, options: IAsyncParallelismOptions & {
weighted: true;
}): Promise<TRetVal[]>;

Parameters

Parameter

Type

Description

iterable

Iterable<TEntry> | AsyncIterable<TEntry>

the array of inputs for the callback function

callback

(entry: TEntry, arrayIndex: number) => Promise<TRetVal>

a function that starts an asynchronous promise for an element from the array

options

IAsyncParallelismOptions & { weighted: true; }

options for customizing the control flow

Returns:

Promise<TRetVal[]>

an array containing the result for each callback, in the same order as the original input array

Remarks

This API is similar to the system Array#map, except that the loop is asynchronous, and the maximum number of concurrent units can be throttled using IAsyncParallelismOptions.concurrency. Using the IAsyncParallelismOptions.weighted option, the weight of each operation can be specified, which determines how many concurrent units it takes up.

If callback throws a synchronous exception, or if it returns a promise that rejects, then the loop stops immediately. Any remaining array items will be skipped, and overall operation will reject with the first error that was encountered.