forEachAsync
Invokes the provided callback function (callback) for each element of the given array, waiting for the Promise returned by the callback to settle.
By default it runs sequentially, calling the next element's callback only after the previous one has completed. Set options.parallel to true to run in parallel. Note that the completion order of each element is not guaranteed when running in parallel.
If a callback throws in sequential mode, the remaining elements are not processed and the returned Promise rejects. In parallel mode, the Promise rejects with the first error, but the other callbacks that have already started keep running.
Code
Interface
typescript
interface ForEachAsyncOptions {
parallel?: boolean;
}
function forEachAsync<T>(
arr: T[] | readonly T[],
callback: (
currentValue: T,
index: number,
arr: T[] | readonly T[]
) => void | Promise<void>,
options?: ForEachAsyncOptions
): Promise<void>;
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
arr | T[] | readonly T[] | - | The array to iterate over. |
callback | (currentValue: T, index: number, arr: T[] | readonly T[]) => void | Promise<void> | - | The function to invoke for each element. |
options.parallel | boolean | false | If true, runs all callbacks in parallel. |
Usage
Sequential execution
typescript
import { forEachAsync } from '@modern-kit/utils';
const ids = [1, 2, 3];
await forEachAsync(ids, async (id, index) => {
await deleteProduct(id);
console.log(index, 'deleted');
// 0 deleted
// 1 deleted
// 2 deleted
});
console.log('All deleted.');
Parallel execution
typescript
import { forEachAsync } from '@modern-kit/utils';
await forEachAsync(
[1, 2, 3],
async (id) => {
await deleteProduct(id);
},
{ parallel: true }
);