@vgerbot/async
    Preparing search index...

    Function transform

    • Transforms a collection into an accumulator value using an async iteratee. Similar to reduce but allows in-place mutation of the accumulator. Useful for building objects or arrays incrementally.

      Type Parameters

      • I

        The type of the input items.

      • R

        The type of the accumulator.

      Parameters

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

        The collection to transform.

      • iteratee: (accumulator: R, item: I, token: CancellableToken) => void | Promise<void>

        An async function that mutates the accumulator for each element.

      • Optionalaccumulator: R

        The initial accumulator value (optional, defaults to [] or {}).

      • Optionaloptions: TransformOptions<R>

        Configuration options, including concurrency limit.

      Returns CancellableHandle<R>

      A cancellable handle that resolves to the final accumulator.

      const handle = transform(
      [1, 2, 3, 4],
      async (result, num, token) => {
      await token.sleep(10);
      if (num % 2 === 0) {
      result.push(num * 2);
      }
      },
      [] as number[],
      );

      const result = await handle; // [4, 8]
      const handle = transform(
      { a: 1, b: 2, c: 3 },
      async (result, value, key) => {
      result[key] = value * 10;
      },
      {} as Record<string, number>,
      );

      const result = await handle; // { a: 10, b: 20, c: 30 }
    • Transforms a collection into an accumulator value using an async iteratee. Similar to reduce but allows in-place mutation of the accumulator. Useful for building objects or arrays incrementally.

      Type Parameters

      • I

        The type of the input items.

      • R

        The type of the accumulator.

      Parameters

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

        The collection to transform.

      • iteratee: (
            accumulator: R,
            item: I,
            key: string | number,
            token: CancellableToken,
        ) => void | Promise<void>

        An async function that mutates the accumulator for each element.

      • Optionalaccumulator: R

        The initial accumulator value (optional, defaults to [] or {}).

      • Optionaloptions: TransformOptions<R>

        Configuration options, including concurrency limit.

      Returns CancellableHandle<R>

      A cancellable handle that resolves to the final accumulator.

      const handle = transform(
      [1, 2, 3, 4],
      async (result, num, token) => {
      await token.sleep(10);
      if (num % 2 === 0) {
      result.push(num * 2);
      }
      },
      [] as number[],
      );

      const result = await handle; // [4, 8]
      const handle = transform(
      { a: 1, b: 2, c: 3 },
      async (result, value, key) => {
      result[key] = value * 10;
      },
      {} as Record<string, number>,
      );

      const result = await handle; // { a: 10, b: 20, c: 30 }