TS-Scribe - v1.2.0
    Preparing search index...

    Function asyncMap

    • Maps over an array with an asynchronous callback function and returns a promise that resolves to an array of results. Rejects on the first error.

      For collecting errors instead of failing fast, use asyncMapSettled.

      Type Parameters

      • T

        The type of elements in the input array

      • R

        The type of elements in the result array

      Parameters

      • array: T[]

        The input array to map over

      • callback: (item: T, index: number, array: T[]) => Promise<R>

        The async function to apply to each element

      • Optionaloptions: { concurrency?: number; signal?: AbortSignal } = {}

        Optional configuration

        • Optionalconcurrency?: number

          Maximum number of concurrent operations

        • Optionalsignal?: AbortSignal

          AbortSignal to cancel processing. When aborted, the promise rejects with an AbortError.

      Returns Promise<R[]>

      Promise resolving to an array of mapped results.

      If concurrency is not a positive integer.

      If the input array is null or undefined.

      If the signal is aborted.

      The first error thrown by the callback.

      // Basic usage
      const numbers = [1, 2, 3, 4];
      const doubled = await asyncMap(numbers, async (n) => n * 2);
      // Result: [2, 4, 6, 8]
      // With limited concurrency
      const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
      const results = await asyncMap(urls, fetchData, { concurrency: 2 });
      // Only 2 requests will run at a time
      // With AbortSignal
      const controller = new AbortController();
      setTimeout(() => controller.abort(), 5000);
      const results = await asyncMap(urls, fetchData, { signal: controller.signal });