@pengzhanbo/utils - v3.4.1
    Preparing search index...

    Function filterAsync

    • Filters an array asynchronously using an async predicate function.

      Returns a promise that resolves to a new array containing only the elements for which the predicate function returns a truthy value.

      使用异步谓词函数对数组进行异步过滤。

      返回一个Promise,该Promise解析为一个新数组,仅包含谓词函数返回真值的元素。

      Type Parameters

      • T

      Parameters

      • array: readonly T[]

        The array to filter. 要过滤的数组

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

        An async predicate function to test each element. 用于测试每个元素的异步谓词函数

      • Optionalconcurrency: number

        The maximum number of concurrent operations. 最大并发操作数

      Returns Promise<T[]>

      A promise that resolves to the filtered array. 解析为过滤后数组的Promise

      const users = [{ id: 1, active: true }, { id: 2, active: false }, { id: 3, active: true }];
      const activeUsers = await filterAsync(users, async (user) => {
      return await checkUserStatus(user.id);
      });
      // Returns: [{ id: 1, active: true }, { id: 3, active: true }]