Tweakr
    Preparing search index...

    Function reduceAsync

    • Asynchronously reduces an array to a single accumulated value.

      Each iteration awaits the result of the provided async reducer function before continuing. This allows for sequential async transformations over array items.

      Type Parameters

      • T

        Type of elements in the input array.

      • R

        Type of the accumulated result.

      Parameters

      • arr: T[]

        The array to iterate over.

      • fn: (acc: R, val: T, index: number, array: T[]) => Promise<R>

        An async reducer function called for each element.

      • initial: R

        The initial accumulator value.

      Returns Promise<R>

      A promise that resolves to the final accumulated result.

      const result = await reduceAsync(
      [1, 2, 3],
      async (acc, val) => acc + val,
      0
      );
      console.log(result); // 6

      1.1.0