Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
es6-promise-pool
Advanced tools
Runs Promise
s in a pool that limits their maximum concurrency.
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.
This module can be used both under Node.js (version 0.10 and up) and on the
Web. In both cases, your platform needs to have the Promise
prototype
available. You can use
ES6-Promise as a polyfill; it
is used internally as well.
npm install --save es6-promise-pool
<script src="es6-promise.js"></script>
<script>ES6Promise.polyfill();</script>
<script src="es6-promise-pool.js"></script>
// On the Web, just leave out this line.
var promisePool = require('es6-promise-pool');
var PromisePool = promisePool.PromisePool;
// Can also be a generator. See below.
var promiseProducer = function() {
// There's a 10% chance that we return null, indicating that there are no
// more promises left to process.
if (Math.floor(Math.random() * 10) === 0) {
console.log('No more promises in pool');
return null;
}
// If we didn't return null, we pass a new promise to the pool.
var promisedValue = new Date().getTime();
console.log('Creating new promise for ' + promisedValue);
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('Resolving promise: ' + promisedValue);
// See below for instructions on how to retrieve the value.
resolve(promisedValue);
}, 1000);
});
};
// The number of promises to process simultaneously.
var concurrency = 3;
// Create a pool.
var pool = new PromisePool(promiseProducer, concurrency);
// Start the pool.
var poolPromise = pool.start();
// Wait for the pool to settle.
poolPromise.then(function() {
console.log('All promises fulfilled');
}, function(error) {
console.log('Some promise rejected: ' + error.message);
});
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 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);
});
};
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 = 0;
var promiseProducer = function() {
if (count < 5) {
count++;
return delayValue(count, 1000);
} else {
return null;
}
};
var pool = new PromisePool(promiseProducer, 3);
pool.start()
.then(function() {
console.log('Complete');
});
We can achieve the same result with ECMAScript 6 generator functions.
var promiseProducer = function*() {
for (var count = 1; count <= 5; count++) {
yield delayValue(count, 1000);
}
};
var pool = new PromisePool(promiseProducer, 3);
pool.start()
.then(function() {
console.log('Complete');
});
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 = new PromisePool(promiseProducer, 3);
pool.addEventListener('fulfilled', function(event) {
// The event contains:
// - target: the PromisePool itself;
// - data:
// - promise: the Promise that got fulfilled;
// - result: the result of that Promise.
console.log('Fulfilled: ' + event.data.result);
});
pool.addEventListener('rejected', function(event) {
// The event contains:
// - target: the PromisePool itself;
// - data:
// - promise: the Promise that got rejected;
// - error: the Error for the rejection.
console.log('Rejected: ' + event.data.error.message);
});
pool.start()
.then(function() {
console.log('Complete');
});
Copyright © 2015 Tim De Pauw
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Runs Promises in a pool that limits their concurrency.
The npm package es6-promise-pool receives a total of 24,249 weekly downloads. As such, es6-promise-pool popularity was classified as popular.
We found that es6-promise-pool demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.