Function reduce

  • Reduces an input iterable to a single value using an asynchronous reducer function.

    Returns

    A function that accepts an input iterable and returns a Promise that resolves to the reduced value.

    Example

    const input = [1, 2, 3];
    const add = async (acc: number, value: number) => acc + value;
    const sum = reduce(add)(input);

    (async () => {
    console.log(await sum); // Logs 6
    })();

    Type Parameters

    • T

      The type of elements in the input iterable.

    • U = T

      The type of the output value. Defaults to T.

    Parameters

    • reducer: AsyncReducer<T, U>

      An asynchronous reducer function that takes the accumulator, a value, and its index.

    • Optional initialValue: U

      Optional. The initial value of the accumulator. If not provided, the first value of the iterable is used.

    Returns ((input: AnyIterable<T>) => Promise<U>)

Generated using TypeDoc