Rush StackShopBlogEvents
Skip to main content

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

Async.forEachAsync() method

This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.

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

Signature:

static forEachAsync<TEntry>(iterable: Iterable<TEntry> | AsyncIterable<TEntry>, callback: (entry: TEntry, arrayIndex: number) => Promise<void>, options?: IAsyncParallelismOptions | undefined): Promise<void>;

Parameters

ParameterTypeDescription
iterableIterable<TEntry> | AsyncIterable<TEntry>the array of inputs for the callback function
callback(entry: TEntry, arrayIndex: number) => Promise<void>a function that starts an asynchronous promise for an element from the array
optionsIAsyncParallelismOptions | undefined(Optional) options for customizing the control flow

Returns:

Promise<void>

Remarks

This API is similar to the system Array#forEach, except that the loop is asynchronous, and the maximum number of concurrent promises can be throttled using IAsyncParallelismOptions.concurrency.

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.