@vgerbot/async
    Preparing search index...

    Function map

    • Maps over an array of data concurrently, applying an asynchronous callback to each element. Allows controlling the maximum concurrency of the mapping operations.

      Type Parameters

      • I

        The type of the input array items.

      • R

      Parameters

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

        The array of data to map over, or a promise that resolves to one.

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

        An async function applied to each element, producing a mapped value.

      • Optionaloptions: MapOptions<R>

        Configuration options, including cancellation token and concurrency limit.

      Returns CancellableHandle<R[]>

      A cancellable handle that resolves to an array of mapped values.

      const handle = map(
      [1, 2, 3],
      async (item, token) => {
      await token.sleep(10);
      return item * 2;
      },
      { concurrency: 2 },
      );

      const result = await handle; // [2, 4, 6]
      const handle = map(
      { a: 1, b: 2 },
      async (value, key) => `${String(key)}:${value * 10}`,
      );

      const result = await handle; // ["a:10", "b:20"]
    • Maps over an array of data concurrently, applying an asynchronous callback to each element. Allows controlling the maximum concurrency of the mapping operations.

      Type Parameters

      • I

        The type of the input array items.

      • R

      Parameters

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

        The array of data to map over, or a promise that resolves to one.

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

        An async function applied to each element, producing a mapped value.

      • Optionaloptions: MapOptions<R>

        Configuration options, including cancellation token and concurrency limit.

      Returns CancellableHandle<R[]>

      A cancellable handle that resolves to an array of mapped values.

      const handle = map(
      [1, 2, 3],
      async (item, token) => {
      await token.sleep(10);
      return item * 2;
      },
      { concurrency: 2 },
      );

      const result = await handle; // [2, 4, 6]
      const handle = map(
      { a: 1, b: 2 },
      async (value, key) => `${String(key)}:${value * 10}`,
      );

      const result = await handle; // ["a:10", "b:20"]