Codementor Events

Limiting the number of promises running concurrently

Published Mar 24, 2018
Limiting the number of promises running concurrently

How-to

You can solve this problem easily using ES6 Promise Pool. Here is a basic example :

var maxParallelRequests = 3;
var count = 0;
var total = 100;

var promiseProducer = function () {
  if (count < total) {
    count++;

    promise = getResultsAsync(`http://my-api.com/${count}`);

    return promise;
  } else {
    return null;
  }
};

var pool = new PromisePool(promiseProducer, maxParallelRequests);

This example will request http://my-api.com/1, http://my-api.com/2, and so on, until http://my-api.com/100, limiting the number of parallel to 3.

It works by using a promise producer function (promiseProducer). This function will be called by PromisePool and should returns a Promise each time, unless it returns null which stops the Promise generation logic. PromisePool will gather all the Promises it has, and execute them limiting parallel calls.

Discover and read more posts from Gwenael Pluchon
get started
post commentsBe the first to share your opinion
Show more replies