Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
E

es6-promise-pool

> 编程语言
Open source

Runs Promises in a pool that limits their concurrency.

379 stars0 likes2 views
WebsiteGitHub

About

Runs Promises in a pool that limits their concurrency.

ES6 Promise Pool

Runs Promises in a pool that limits their concurrency.

Motivation

An ECMAScript 2015 Promise is a great way to handle 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. Alternatively, you can pass an ES2015 iterator, meaning you can also use generator functions.

Compatibility

This module can be used both under Node.js and on the Web. If your platform does not have a native Promise implementation, you can use a polyfill such as ES6-Promise.

Installation

bash
npm install --save es6-promise-pool
bash
bower install --save es6-promise-pool
xml

Usage

…

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:

javascript
var delayValue = function (value, time) {
  return new Promise(function (resolve, reject) {
    console.log('Resolving ' + value + ' in ' + time + ' ms')
    setTimeout(function () {
      console.log('Resolving: ' + value)
      resolve(value)
    }, time)
  })
}

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.

javascript
var count = 0
var promiseProducer = function () {
  if (count  console.log('Complete'))

It's also possible to pass a generator function directly. In that case, it will be invoked with no arguments and the resulting iterator will be used. This feature will however be removed in version 3.

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.

…

Upgrading

Since version 2.0.0, this module does not depend on ES6-Promise anymore. If you want to support platforms without a native Promise implementation, please load a polyfill first.

If you prefer not to polyfill the global Promise for whatever reason, you can also pass your Promise class as an option instead:

javascript
var ES6Promise = require('es6-promise').Promise // or another implementation
var pool = new PromisePool(promiseProducer, concurrency, {promise: ES6Promise})

Alternatives

  • Vilic Vane's Promise Pool offers a similar API.
  • Bluebird includes Promise.map(), which takes a concurrency option.
  • Similarly, λ (a.k.a. contra) has λ.concurrent() with the optional cap parameter.
  • With Q, you can use qlimit.
  • Async does not use promises, but offers a queue() function.

Author

Tim De Pauw

License

MIT

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言