ES6 Promise Pool
Runs Promise
s in a pool that limits their maximum concurrency.
Motivation
An ECMAScript 6 Promise
is a great way of handling asynchronous operations.
The Promise.all
function provides an easy interface to let a bunch of promises
settle concurrently.
However, it's an all-or-nothing approach: all your promises get created
simultaneously. If you have a ton of operations that you want to run with some
concurrency, Promise.all
is no good.
Instead, you probably want to limit the maximum number of simultaneous operations. That's where this module comes in. It provides an easy way of waiting for any number of promises to settle, while imposing an upper bound on the number of simultaneously executing promises.
The promises can be created in a just-in-time fashion. You essentially pass a function that produces a new promise every time it is called. On modern platforms, you can also use ES6 generator functions for this.
Compatibility
This module can be used both under Node.js (version 0.10 and up) and on the
Web. If your platform does not have a Promise
implementation, it will be
polyfilled by ES6-Promise.
Installation
npm install --save es6-promise-pool
bower install --save es6-promise-pool
Usage
// On the Web, leave out this line and use the script tags above instead.var promisePool = var PromisePool = promisePoolPromisePool var { // Your code goes here. // If there is work left to be done, return the next work item as a promise. // Otherwise, return null to indicate that all promises have been created. // Scroll down for an example.} // The number of promises to process simultaneously.var concurrency = 3 // Create a pool.var pool = promiseProducer concurrency // Start the pool.var poolPromise = poolstart // Wait for the pool to settle.poolPromise
Producers
The PromisePool
constructor takes a Promise
-producing function as its first
argument. Let's first assume that we have this helper function that returns a
promise for the given value
after time
milliseconds:
var { return { console }}
Function
Now, let's use the helper function above to create five such promises, which
are each fulfilled after a second. Because of the concurrency
of 3
, the
first three promises will be fulfilled after one second. Then, the remaining two
will be processed and fulfilled after another second.
var count = 0var { if count < 5 count++ return else return null } var pool = promiseProducer 3 poolstart
Generator
We can achieve the same result with ECMAScript 6 generator functions.
const promiseProducer = { for let count = 1; count <= 5; count++ } const pool = promiseProducer 3 poolstart
Events
We can also ask the promise pool to notify us when an individual promise is
fulfilled or rejected. The pool fires fulfilled
and rejected
events exactly
for this purpose.
var pool = promiseProducer concurrency pool pool poolstart
Alternatives
- Vilic Vane's Promise Pool offers a similar API.
- Bluebird includes
Promise.map()
, which takes aconcurrency
option. - With Q, you can use qlimit.
- Async does not use promises, but offers a
queue()
function.
Author
License
MIT