Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
promise-queue
Advanced tools
The promise-queue npm package is designed to manage the execution of Promises in a queue with a configurable concurrency limit. This means that it allows for the sequential or controlled concurrent execution of asynchronous operations, which is particularly useful in scenarios where executing too many operations at once could lead to resource exhaustion or when the order of operations matters.
Creating a queue and adding tasks
This code demonstrates how to create a new queue with a maximum of 2 concurrent operations and an infinite queue size. It then adds an asynchronous function to the queue and logs the result when the promise resolves.
const Queue = require('promise-queue');
let maxConcurrent = 2;
let maxQueue = Infinity;
let queue = new Queue(maxConcurrent, maxQueue);
function someAsyncFunction() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('done');
}, 1000);
});
}
queue.add(someAsyncFunction).then(console.log);
Handling results and errors
This snippet shows how to handle both successful results and errors for tasks added to the queue. It uses `.then` for success callbacks and `.catch` for error handling.
queue.add(someAsyncFunction).then(result => {
console.log('Result:', result);
}).catch(error => {
console.error('Error:', error);
});
Getting the size of the queue
These lines of code demonstrate how to get the number of queued and pending promises in the queue. `getQueueLength` returns the number of queued (not yet started) tasks, while `getPendingLength` returns the number of tasks currently running.
console.log(queue.getQueueLength());
console.log(queue.getPendingLength());
p-queue is a similar package that also manages promise execution with concurrency control. Compared to promise-queue, p-queue offers more advanced features such as priority levels for tasks, pausing and resuming the queue, and better overall flexibility in managing tasks.
The async package provides a wide range of functions for working with asynchronous JavaScript, including queue management. While not exclusively focused on promises (it supports both callbacks and promises), it offers a similar queue functionality with concurrency control. It's more versatile but can be more complex to use for promise-specific scenarios.
Bottleneck is another npm package designed to rate limit the execution of functions. It's similar to promise-queue in that it can help manage the execution rate of asynchronous operations, but it focuses more on limiting the execution rate to avoid hitting rate limits or overloading resources, rather than managing a queue of tasks.
[] (https://npmjs.org/package/promise-queue) [] (https://travis-ci.org/azproduction/promise-queue) [] (https://coveralls.io/r/azproduction/promise-queue) [] (https://gemnasium.com/azproduction/promise-queue)
Promise-based queue
promise-queue
can be installed using npm
:
npm install promise-queue
Queue.configure(function () {
return $.Deferred() || require('vow').promise();
});
// max concurrent - 1
// max queue - Infinity
var queue = new Queue(1, Infinity);
queue.add(function () {
// resolve of this promise will resume next request
return downloadTarballFromGithub(url, file);
})
.then(function (file) {
doStuffWith(file);
});
queue.add(function () {
return downloadTarballFromGithub(url, file);
})
// This request will be paused
.then(function (file) {
doStuffWith(file);
});
FAQs
Promise-based queue
We found that promise-queue 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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.