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

    Function mapAsync

    • Transforms each element in an array using an async callback function and returns a promise that resolves to an array of transformed values.

      使用异步回调函数转换数组中的每个元素,并返回一个解析为转换后值数组的 promise。

      Type Parameters

      • T

        The type of elements in the array / 数组元素的类型

      • R = T

        The type of the transformed result / 转换结果的类型

      Parameters

      • array: readonly T[]

        The array to transform / 要转换的数组

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

        The async callback function to apply to each element / 对每个元素应用的异步回调函数

      • Optionalconcurrency: number

        The maximum number of concurrent operations to allow / 允许的最大并发操作数

      Returns Promise<R[]>

      A promise that resolves to an array of transformed values / 解析为转换后值数组的 promise

      Transform functions are executed concurrently via promiseParallel under the hood. The results maintain the original array order regardless of the order in which transforms resolve. The concurrency parameter controls the maximum number of simultaneously pending operations.

      转换函数通过底层的 promiseParallel并发执行。 无论转换函数的解析顺序如何,结果都保持原始数组的顺序。 concurrency参数控制同时进行中的最大操作数。

      filterAsync — for async filtering / 异步过滤

      const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
      const userDetails = await mapAsync(users, async (user) => {
      return await fetchUserDetails(user.id);
      });
      // Returns: [{ id: 1, name: '...' }, { id: 2, name: '...' }, { id: 3, name: '...' }]
      // With concurrency limit
      const numbers = [1, 2, 3, 4, 5];
      const results = await mapAsync(
      numbers,
      async (n) => await slowOperation(n),
      2
      );
      // Processes at most 2 operations concurrently