Simple Javascript Promise pool

Ágoston Fung
2 min readFeb 16, 2021

--

Build one for yourself instead of using a library

The problem

Recently at work, I received a task to collect data using our REST APIs. I received a list of product IDs and I had to fetch the detailes from an endpoint. After that, I needed to pick the category field and invoke another API to receive the cost center. Very simple task.

I implemented my first solution, but I needed to improve it over time. I tried the following approaches:

  • Wait for each request to finish: very simple solution but slow
  • Use Promise.all for asynchronous processing: very fast but it may overflow the server with requests and that could cause timeouts
  • Use a pool of Promises: the most efficient way as it guarantees the number of concurrent requests to the server

Using a library

Naturally, I went googling for a suitable library. There are many options to choose from but not one solution seemed to be simple enough for my needs.

A few libraries I found that other people seem to be using:

Implement it yourself

It turns out that implementing it yourself is not that hard. You just need to use an appropriate data structure to model your pool. In my case, I used a simple object, where each key would be an ID to process and the value would be the Promise fulfilling that request.

The other catch was to use Promise.race() to find the first Promise that has finished.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ágoston Fung
Ágoston Fung

Responses (2)

Write a response