source: node_modules/es-toolkit/dist/array/reduceAsync.d.mts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/**
2 * Reduces an array to a single value using an async reducer function.
3 *
4 * Applies the reducer function sequentially to each element (left to right),
5 * carrying an accumulated result from one call to the next. Unlike other async
6 * array methods, reduce must process elements sequentially and does not support
7 * concurrency limiting.
8 *
9 * @template T - The type of elements in the array.
10 * @template U - The type of the accumulated result.
11 * @param {readonly T[]} array The array to reduce.
12 * @param {(accumulator: U, currentValue: T, currentIndex: number, array: readonly T[]) => Promise<U>} reducer An async function that processes each element.
13 * @param {U} initialValue The initial value of the accumulator.
14 * @returns {Promise<U>} A promise that resolves to the final accumulated value.
15 * @example
16 * const numbers = [1, 2, 3, 4, 5];
17 * const sum = await reduceAsync(
18 * numbers,
19 * async (acc, n) => acc + await fetchValue(n),
20 * 0
21 * );
22 * // Returns: sum of all fetched values
23 *
24 * @example
25 * const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
26 * const userMap = await reduceAsync(
27 * users,
28 * async (acc, user) => {
29 * const details = await fetchUserDetails(user.id);
30 * acc[user.id] = details;
31 * return acc;
32 * },
33 * {} as Record<number, any>
34 * );
35 * // Returns: { 1: {...}, 2: {...}, 3: {...} }
36 */
37declare function reduceAsync<T, U>(array: readonly T[], reducer: (accumulator: U, currentValue: T, currentIndex: number, array: readonly T[]) => Promise<U>, initialValue: U): Promise<U>;
38/**
39 * Reduces an array to a single value using an async reducer function.
40 *
41 * Applies the reducer function sequentially to each element (left to right),
42 * carrying an accumulated result from one call to the next. Unlike other async
43 * array methods, reduce must process elements sequentially and does not support
44 * concurrency limiting.
45 *
46 * When no initial value is provided, the first element of the array is used as
47 * the initial value and the reduction starts from the second element.
48 *
49 * @template T - The type of elements in the array.
50 * @param {readonly T[]} array The array to reduce.
51 * @param {(accumulator: T, currentValue: T, currentIndex: number, array: readonly T[]) => Promise<T>} reducer An async function that processes each element.
52 * @returns {Promise<T | undefined>} A promise that resolves to the final accumulated value, or undefined if the array is empty.
53 * @example
54 * const numbers = [1, 2, 3, 4, 5];
55 * const sum = await reduceAsync(
56 * numbers,
57 * async (acc, n) => acc + n
58 * );
59 * // Returns: 15
60 *
61 * @example
62 * const emptyArray: number[] = [];
63 * const result = await reduceAsync(
64 * emptyArray,
65 * async (acc, n) => acc + n
66 * );
67 * // Returns: undefined
68 */
69declare function reduceAsync<T>(array: readonly T[], reducer: (accumulator: T, currentValue: T, currentIndex: number, array: readonly T[]) => Promise<T>): Promise<T>;
70
71export { reduceAsync };
Note: See TracBrowser for help on using the repository browser.