| 1 | interface MapAsyncOptions {
|
|---|
| 2 | concurrency?: number;
|
|---|
| 3 | }
|
|---|
| 4 | /**
|
|---|
| 5 | * Transforms each element in an array using an async callback function and returns
|
|---|
| 6 | * a promise that resolves to an array of transformed values.
|
|---|
| 7 | *
|
|---|
| 8 | * @template T - The type of elements in the input array.
|
|---|
| 9 | * @template R - The type of elements in the output array.
|
|---|
| 10 | * @param {readonly T[]} array The array to transform.
|
|---|
| 11 | * @param {(item: T, index: number, array: readonly T[]) => Promise<R>} callback An async function that transforms each element.
|
|---|
| 12 | * @param {MapAsyncOptions} [options] Optional configuration object.
|
|---|
| 13 | * @param {number} [options.concurrency] Maximum number of concurrent async operations. If not specified, all operations run concurrently.
|
|---|
| 14 | * @returns {Promise<R[]>} A promise that resolves to an array of transformed values.
|
|---|
| 15 | * @example
|
|---|
| 16 | * const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|---|
| 17 | * const userDetails = await mapAsync(users, async (user) => {
|
|---|
| 18 | * return await fetchUserDetails(user.id);
|
|---|
| 19 | * });
|
|---|
| 20 | * // Returns: [{ id: 1, name: '...' }, { id: 2, name: '...' }, { id: 3, name: '...' }]
|
|---|
| 21 | *
|
|---|
| 22 | * @example
|
|---|
| 23 | * // With concurrency limit
|
|---|
| 24 | * const numbers = [1, 2, 3, 4, 5];
|
|---|
| 25 | * const results = await mapAsync(
|
|---|
| 26 | * numbers,
|
|---|
| 27 | * async (n) => await slowOperation(n),
|
|---|
| 28 | * { concurrency: 2 }
|
|---|
| 29 | * );
|
|---|
| 30 | * // Processes at most 2 operations concurrently
|
|---|
| 31 | */
|
|---|
| 32 | declare function mapAsync<T, R>(array: readonly T[], callback: (item: T, index: number, array: readonly T[]) => Promise<R>, options?: MapAsyncOptions): Promise<R[]>;
|
|---|
| 33 |
|
|---|
| 34 | export { mapAsync };
|
|---|