What is the best way to limit concurrency when using ES6's Promise.all()?

前端 未结 17 755
执念已碎
执念已碎 2020-11-29 21:28

I have some code that is iterating over a list that was queried out of a database and making an HTTP request for each element in that list. That list can sometimes be a rea

17条回答
  •  执笔经年
    2020-11-29 21:48

    I suggest the library async-pool: https://github.com/rxaviers/async-pool

    npm install tiny-async-pool
    

    Description:

    Run multiple promise-returning & async functions with limited concurrency using native ES6/ES7

    asyncPool runs multiple promise-returning & async functions in a limited concurrency pool. It rejects immediately as soon as one of the promises rejects. It resolves when all the promises completes. It calls the iterator function as soon as possible (under concurrency limit).

    Usage:

    const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
    await asyncPool(2, [1000, 5000, 3000, 2000], timeout);
    // Call iterator (i = 1000)
    // Call iterator (i = 5000)
    // Pool limit of 2 reached, wait for the quicker one to complete...
    // 1000 finishes
    // Call iterator (i = 3000)
    // Pool limit of 2 reached, wait for the quicker one to complete...
    // 3000 finishes
    // Call iterator (i = 2000)
    // Itaration is complete, wait until running ones complete...
    // 5000 finishes
    // 2000 finishes
    // Resolves, results are passed in given array order `[1000, 5000, 3000, 2000]`.
    

提交回复
热议问题