| 1 | interface FlatMapAsyncOptions {
|
|---|
| 2 | concurrency?: number;
|
|---|
| 3 | }
|
|---|
| 4 | /**
|
|---|
| 5 | * Maps each element in an array using an async callback function and flattens the result by one level.
|
|---|
| 6 | *
|
|---|
| 7 | * This is equivalent to calling `mapAsync` followed by `flat(1)`, but more efficient.
|
|---|
| 8 | * Each callback should return an array, and all returned arrays are concatenated into
|
|---|
| 9 | * a single output array.
|
|---|
| 10 | *
|
|---|
| 11 | * @template T - The type of elements in the input array.
|
|---|
| 12 | * @template R - The type of elements in the arrays returned by the callback.
|
|---|
| 13 | * @param {readonly T[]} array The array to transform.
|
|---|
| 14 | * @param {(item: T, index: number, array: readonly T[]) => Promise<R[]>} callback An async function that transforms each element into an array.
|
|---|
| 15 | * @param {FlatMapAsyncOptions} [options] Optional configuration object.
|
|---|
| 16 | * @param {number} [options.concurrency] Maximum number of concurrent async operations. If not specified, all operations run concurrently.
|
|---|
| 17 | * @returns {Promise<R[]>} A promise that resolves to a flattened array of transformed values.
|
|---|
| 18 | * @example
|
|---|
| 19 | * const users = [{ id: 1 }, { id: 2 }];
|
|---|
| 20 | * const allPosts = await flatMapAsync(users, async (user) => {
|
|---|
| 21 | * return await fetchUserPosts(user.id);
|
|---|
| 22 | * });
|
|---|
| 23 | * // Returns: [post1, post2, post3, ...] (all posts from all users)
|
|---|
| 24 | *
|
|---|
| 25 | * @example
|
|---|
| 26 | * // With concurrency limit
|
|---|
| 27 | * const numbers = [1, 2, 3];
|
|---|
| 28 | * const results = await flatMapAsync(
|
|---|
| 29 | * numbers,
|
|---|
| 30 | * async (n) => await fetchRelatedItems(n),
|
|---|
| 31 | * { concurrency: 2 }
|
|---|
| 32 | * );
|
|---|
| 33 | * // Processes at most 2 operations concurrently
|
|---|
| 34 | */
|
|---|
| 35 | declare function flatMapAsync<T, R>(array: readonly T[], callback: (item: T, index: number, array: readonly T[]) => Promise<R[]>, options?: FlatMapAsyncOptions): Promise<R[]>;
|
|---|
| 36 |
|
|---|
| 37 | export { flatMapAsync };
|
|---|