| 1 | interface ForEachAsyncOptions {
|
|---|
| 2 | concurrency?: number;
|
|---|
| 3 | }
|
|---|
| 4 | /**
|
|---|
| 5 | * Executes an async callback function for each element in an array.
|
|---|
| 6 | *
|
|---|
| 7 | * Unlike the native `forEach`, this function returns a promise that resolves
|
|---|
| 8 | * when all async operations complete. It supports optional concurrency limiting.
|
|---|
| 9 | *
|
|---|
| 10 | * @template T - The type of elements in the array.
|
|---|
| 11 | * @param {readonly T[]} array The array to iterate over.
|
|---|
| 12 | * @param {(item: T, index: number, array: readonly T[]) => Promise<void>} callback An async function to execute for each element.
|
|---|
| 13 | * @param {ForEachAsyncOptions} [options] Optional configuration object.
|
|---|
| 14 | * @param {number} [options.concurrency] Maximum number of concurrent async operations. If not specified, all operations run concurrently.
|
|---|
| 15 | * @returns {Promise<void>} A promise that resolves when all operations complete.
|
|---|
| 16 | * @example
|
|---|
| 17 | * const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|---|
| 18 | * await forEachAsync(users, async (user) => {
|
|---|
| 19 | * await updateUser(user.id);
|
|---|
| 20 | * });
|
|---|
| 21 | * // All users have been updated
|
|---|
| 22 | *
|
|---|
| 23 | * @example
|
|---|
| 24 | * // With concurrency limit
|
|---|
| 25 | * const items = [1, 2, 3, 4, 5];
|
|---|
| 26 | * await forEachAsync(
|
|---|
| 27 | * items,
|
|---|
| 28 | * async (item) => await processItem(item),
|
|---|
| 29 | * { concurrency: 2 }
|
|---|
| 30 | * );
|
|---|
| 31 | * // Processes at most 2 items concurrently
|
|---|
| 32 | */
|
|---|
| 33 | declare function forEachAsync<T>(array: readonly T[], callback: (item: T, index: number, array: readonly T[]) => Promise<void>, options?: ForEachAsyncOptions): Promise<void>;
|
|---|
| 34 |
|
|---|
| 35 | export { forEachAsync };
|
|---|