TS-Scribe - v0.6.2
    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.

      Type Parameters

      • T

        The type of elements in the input array

      • R

        The type of elements in the result array

      • E = undefined

        The type of the error value (defaults to undefined)

      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; continueOnError?: boolean; errorValue?: E } = {}

        Optional configuration

        • Optionalconcurrency?: number

          Maximum number of concurrent operations

        • OptionalcontinueOnError?: boolean

          Whether to continue execution when a callback throws

        • OptionalerrorValue?: E

          Value to use when an error occurs and continueOnError is true

      Returns Promise<(R | E)[]>

      Promise resolving to an array of results or error values

      // 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 error handling
      const ids = [1, 2, 3, 4, 5];
      const users = await asyncMap(ids, fetchUser, {
      continueOnError: true,
      errorValue: { error: true, message: 'User not found' }
      });
      // If fetchUser fails for any id, that result will be the errorValue