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, concurrency?: number): Promise<void>
ParameterTypeDescription
arrayreadonly T[]The array to iterate
fnfunctionAsync (or sync) function called with `(item, index)`
concurrencynumberMaximum number of concurrent calls. Defaults to unlimited. (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

View source on GitHub