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

    Function asyncMapSettled

    • Maps over an array with an asynchronous callback function, collecting both results and errors. Unlike asyncMap, this function never throws due to callback errors — failed items are replaced with errorValue (default undefined) in the results array and their errors are collected in the returned errors array.

      Use this when you want all items processed regardless of individual failures.

      Type Parameters

      • T

        The type of elements in the input array

      • R

        The type of successfully mapped elements

      • 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; errorValue?: E; signal?: AbortSignal } = {}

        Optional configuration

        • Optionalconcurrency?: number

          Maximum number of concurrent operations

        • OptionalerrorValue?: E

          Value to use in the results array when a callback throws

        • Optionalsignal?: AbortSignal

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

      Returns Promise<AsyncMapSettledResult<R, E>>

      Promise resolving to an object with results and errors.

      If concurrency is not a positive integer.

      If the input array is null or undefined.

      If the signal is aborted.

      // Basic usage
      const { results, errors } = await asyncMapSettled(urls, fetchPage);
      console.log('Succeeded:', results.filter(r => r !== undefined));
      console.log('Failed:', errors);
      // With custom error value
      const { results, errors } = await asyncMapSettled(ids, fetchUser, {
      errorValue: { error: true, message: 'Not found' }
      });