Tweakr
    Preparing search index...

    Function mapAsync

    • Asynchronously maps over an array using a provided async function, with optional concurrency control.

      Executes up to concurrency mapping operations in parallel until all items are processed. Ensures results maintain the original array order.

      Type Parameters

      • T

        The type of elements in the input array.

      • R

        The type of elements in the resulting array.

      Parameters

      • arr: T[]

        The array to iterate over.

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

        The async function invoked per element.

      • concurrency: number = Infinity

        The maximum number of concurrent async operations. Defaults to Infinity.

      Returns Promise<R[]>

      A promise that resolves to an array of mapped results.

      const fetchNumber = async (n: number) => n * 2;
      await mapAsync([1, 2, 3, 4], fetchNumber, 2);
      // → [2, 4, 6, 8]

      1.0.0