@vgerbot/async
    Preparing search index...

    Function reduce

    • Iterates over an array of data, applying an asynchronous reducer function sequentially.

      Type Parameters

      • I

        The type of the input items.

      • R

        The type of the accumulated result.

      Parameters

      • data: I[] | Promise<I[]>

        The array of data to reduce.

      • reducer: (acc: R, item: I, token: CancellableToken) => Promise<R>

        An async function applied to each element, receiving the accumulator, the current item, and a cancellation token.

      • initialValue: R

        The initial value for the accumulator.

      • Optionaloptions: CancellableOptions<unknown>

        Cancellable configuration options.

      Returns CancellableHandle<R>

      A cancellable handle that resolves with the final accumulated value.

      const handle = reduce(
      [1, 2, 3],
      async (acc, item, token) => {
      await token.sleep(5);
      return acc + item;
      },
      0,
      );

      const result = await handle; // 6
      const handle = reduce(
      { a: 1, b: 2 },
      async (acc, value, key) => `${acc}${String(key)}=${value};`,
      "",
      );

      const result = await handle; // "a=1;b=2;"
    • Iterates over an array of data, applying an asynchronous reducer function sequentially.

      Type Parameters

      • I

        The type of the input items.

      • R

        The type of the accumulated result.

      Parameters

      • data: CollectionInput<I> | Promise<CollectionInput<I>>

        The array of data to reduce.

      • reducer: (acc: R, item: I, key: string | number, token: CancellableToken) => Promise<R>

        An async function applied to each element, receiving the accumulator, the current item, and a cancellation token.

      • initialValue: R

        The initial value for the accumulator.

      • Optionaloptions: CancellableOptions<unknown>

        Cancellable configuration options.

      Returns CancellableHandle<R>

      A cancellable handle that resolves with the final accumulated value.

      const handle = reduce(
      [1, 2, 3],
      async (acc, item, token) => {
      await token.sleep(5);
      return acc + item;
      },
      0,
      );

      const result = await handle; // 6
      const handle = reduce(
      { a: 1, b: 2 },
      async (acc, value, key) => `${acc}${String(key)}=${value};`,
      "",
      );

      const result = await handle; // "a=1;b=2;"