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

    Function asyncFilter

    • Asynchronously filters an array based on an asynchronous predicate function. Only elements for which the predicate returns true are included in the result. Rejects on the first error.

      For collecting errors instead of failing fast, use asyncFilterSettled.

      Type Parameters

      • T

        The type of elements in the input array

      Parameters

      • array: T[]

        The array of elements to filter.

      • predicate: (element: T, index: number, array: T[]) => Promise<boolean>

        The asynchronous predicate function that will be executed for each element. Should return true to include the element, false to exclude it.

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

        Optional configuration

        • Optionalconcurrency?: number

          Maximum number of concurrent operations

        • Optionalsignal?: AbortSignal

          AbortSignal to cancel processing.

      Returns Promise<T[]>

      A Promise that resolves to the filtered array.

      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 predicate.

      // Basic usage
      const numbers = [1, 2, 3, 4, 5];
      const evens = await asyncFilter(numbers, async (n) => n % 2 === 0);
      // Result: [2, 4]
      // With limited concurrency
      const items = [...Array(100).keys()];
      const validated = await asyncFilter(items, validateItem, { concurrency: 5 });
      // With AbortSignal
      const controller = new AbortController();
      setTimeout(() => controller.abort(), 5000);
      const results = await asyncFilter(urls, checkUrl, { signal: controller.signal });