Running async functions in parallel with capped concurrency

With async-pool, it's possible to run async functions in parallel, but with capped concurrency.

For example, let's say we have 10,000 users and we need to run an API request for each user. We can run API requests sequentially, but that is too slow for our needs. We can use Promise.all, but the avalanche of API requests is more than the server can handle.

What we can do is to cap the concurrency, that is, to limit the number of concurrent requests. Let's say we set the cap to 25:

import asyncPool from "tiny-async-pool"

const users = //... array of 10,000 user objects  
await asyncPool(25, users, async (user) => callApi(user)) // the cap is 25

Would you like to connect? Subscribe via email or RSS , or follow me on Twitter!