For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/array/forEachAsync.md.

forEachAsync es-toolkit

Executes an async function for each element in an array.

await forEachAsync(array, callback);

Reference

forEachAsync(array, callback, options?)

Use forEachAsync to perform async operations with side effects for each element in an array. Unlike regular forEach, it returns a promise that resolves when all async operations complete.

import { forEachAsync } from 'es-toolkit/array';

// Update all user information.
const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
await forEachAsync(users, async user => {
  await updateUser(user.id);
});
// All user updates completed.

// Limit concurrency.
const items = [1, 2, 3, 4, 5];
await forEachAsync(items, async item => await processItem(item), { concurrency: 2 });
// Only 2 items are processed concurrently at most.

The concurrency option allows you to limit concurrent executions to control load on servers or databases. It's useful for operations that don't return values, like logging, file uploads, or database updates.

import { forEachAsync } from 'es-toolkit/array';

// Upload files sequentially.
const files = ['file1.txt', 'file2.txt', 'file3.txt'];
await forEachAsync(files, async file => await uploadFile(file), { concurrency: 1 });
// Only one file is uploaded at a time.

Parameters

arrayreadonly T[]required

The array to iterate over.

callback(item: T, index: number, array: readonly T[]) => Promise<void>required

An async function to execute for each element.

optionsForEachAsyncOptionsoptional

Options to control concurrency.

  • concurrency (number, optional): Maximum number of concurrent operations. If not specified, all operations run concurrently.

Returns

A promise that resolves when all operations complete.

Promise<void>
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.