1.0.0 • Published 7 years ago

concurrent-promises v1.0.0

Weekly downloads
4
License
ISC
Repository
github
Last release
7 years ago

Concurrent-promises

Build Status

Sometimes we want to limit the number of concurrent promises like in HTTP requests or I/O. This library allows limit the number of concorrency. Different from libraries like: https://github.com/timdp/es6-promise-pool it allows retries and total control over the concurrency flow.

Example

The example below showns how this lib works. You will need a resolver which is the function that will called recursively by concurrentPromises and the number of concurrent promises that will run (default is 8).

In this case are created 10 promises that are resolved in a different time to simulate an assincronous I/O. To finish the conrrentPromises caller you should return null;

    it('should limit of concurrent requests', () => {
      let count = 0;
      const resolver = () => {
        return new Promise(resolve => {
          if(count < 10) {
            count++;
            return setTimeout(() => {
              resolve(true);
            }, Math.floor((Math.random() * 1000) + 1));
          }
          return resolve(null);
        });
      };

      const concurrentPromises = new ConcurrentPromises({resolver, concurrency: 3});

      return concurrentPromises.begin()
        .then(result => {
          expect(result.length).to.be.eql(10);
        });
    });