Skip to content

forEachAsync

The async counterpart to Array.prototype.forEach: runs fn for every item for its side effects, discarding any return value. Prefer mapAsync when you need the results — this only exists to signal that intent clearly, same as the sync forEach/map pair.

Available since v3.0.4

import { forEachAsync } from '@helpers4/array';
// or, from the all-in-one package (same code, one install):
import { forEachAsync } from 'helpers4/array';
forEachAsync<T>(array: readonly T[], fn: function, options?: number | AsyncArrayOptions): Promise<void>
ParameterTypeDescription
arrayreadonly T[]The array to iterate
fnfunctionAsync (or sync) function called with `(item, index)`
optionsnumber | AsyncArrayOptionsMaximum number of concurrent calls (defaults to unlimited), or the options with a `signal` to stop — see AsyncArrayOptions (optional)

Promise<void> — A promise that resolves once every call has settled

Runs fn for its side effects only; at most concurrency calls at once.

await forEachAsync(files, (file) => uploadFile(file), 3)
// uploads at most 3 files concurrently

Options for mapAsync, mapSettled, filterAsync and forEachAsync.

interface AsyncArrayOptions {
  concurrency?: number;
  signal?: AbortSignalLike;
}

View source on GitHub