函数 parallelLimit

  • 动态并发执行函数

    类型参数

    • T
    • R

    参数

    • taskList: T[]

      任务列表

    • maxConcurrency: number

      最大并发数

    • processor: ((task: T) => Promise<R>)

      任务处理函数

        • (task): Promise<R>
        • 参数

          • task: T

          返回 Promise<R>

    返回 Promise<R[]>

    已完成任务结果数组

     const taskList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    const maxConcurrency = 3
    const processor = async (task: number) => {
    await new Promise(resolve => setTimeout(resolve, 1000))
    return task * 2
    }
    const results = await parallelLimit(taskList, maxConcurrency, processor)
    console.log(results) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]