You might be familiar with libraries like p-limit, async or bottleneck. They help you run asynchronous tasks with a concurrency limit. This is useful

Run Concurrent Tasks With a Limit Using Pure JavaScript

submited by
Style Pass
2022-05-15 01:30:08

You might be familiar with libraries like p-limit, async or bottleneck. They help you run asynchronous tasks with a concurrency limit. This is useful when you don't want to overwhelm an API or want to keep resource usage below the maximum threshold.

Using a library is convenient, but it is yet another addition to an already long list of dependencies that your application relies on.

I'm going to teach you a simple and elegant way to run async tasks with a concurrency limit so you don't have to rely on another library.

With that in mind, we can have several loops going through an iterator at the same time knowing that each loop will process a different value.

When we have to run many tasks, it's oftentimes because we have an array that holds some type of value for each task — a list of URLs we want to fetch, or an image collection we want to process. To get a consumable iterator from an array you can use the .values() method on the array.

If we then create an array with size X (= concurrency limit) and fill it with the same iterator, we can map over the array and start off X concurrent loops that go through the iterator.

Leave a Comment