![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
The 'queue' npm package is a fast, robust, and extensible queue implementation for managing a list of tasks in a sequential manner. It allows for asynchronous task processing, concurrency control, timeout for tasks, and pausing/resuming the queue. This package is particularly useful for rate-limiting tasks or operations that need to be executed in order but might have asynchronous results, such as API calls, file processing, or any task that requires throttling.
Basic Queue Functionality
This demonstrates how to create a basic queue, add tasks to it, and start processing. Each task is a function that accepts a callback, which must be called upon completion.
const queue = require('queue');
const q = queue();
q.push(function(cb) {
console.log('Hello');
cb();
});
q.push(function(cb) {
console.log('World');
cb();
});
q.start(function(err) {
console.log('All tasks finished.');
});
Concurrency Control
This example shows how to set a concurrency limit, allowing up to 2 tasks to be processed simultaneously.
const q = queue({concurrency: 2});
// Add tasks to q
q.start(function(err) {
console.log('All tasks processed with a maximum of 2 tasks concurrently.');
});
Timeout for Tasks
This code sets a timeout for each task in the queue. If a task does not call its callback within the specified timeout, the queue will move on to the next task.
const q = queue();
q.timeout = 1000; // 1 second timeout for each task
q.push(function(cb) {
setTimeout(function() {
console.log('This task will timeout');
cb();
}, 1500); // This task takes longer than the timeout
});
q.start();
The 'async' package provides a wide array of functionalities for working with asynchronous JavaScript, including queue management. Compared to 'queue', 'async' offers more comprehensive control over asynchronous flow control but might be more complex for simple queue needs.
Bull is a Redis-backed queue package for handling distributed jobs and messages in Node.js. It's more suited for scenarios requiring robustness, such as background processing or job scheduling, and offers features like prioritization, repeatable jobs, and event listeners. It's more complex and feature-rich compared to 'queue', which is simpler and doesn't require Redis.
p-queue is a promise-based queue with concurrency control, similar to 'queue' but leveraging Promises for task handling. It provides an easy-to-use API for managing asynchronous tasks with more modern JavaScript syntax. It's a good alternative if you prefer working with Promises over callbacks.
____ _ _____ _ _____
/ _ \/ \ /\/ __// \ /\/ __/
| / \|| | ||| \ | | ||| \
| \_\|| \_/|| /_ | \_/|| /_
\____\\____/\____\\____/\____\
An async job queue with adjustable concurrency.
Wanted something more flexible than async's queue.
The module exports a class named Queue
that implements most of the Array api. Pass async functions (ones that accept a callback) to an instance's push()
method. Processing begins automatically on process.nextTick()
.
npm install queue
concurrency
maximum number of jobs that the queue should process concurrently - the default is 1timeout
milliseconds to wait for a job to execute its callbackpush(job)
add a job to the queue'processed'
when jobs finish'timeout'
when queue.timeout
milliseconds have elapsed and a job has not executed its callback'drain'
when the queue finishes processing all its jobsvar Queue = require('queue');
var q = new Queue({
timeout: 100,
concurrency: 100
});
var results = [];
// listen for events
q.on('processed', function(job) {
console.log('job finished processing:', job.toString().replace(/\n/g, ''));
});
q.on('drain', function() {
console.log('all done:', results);
});
// add jobs using familiar Array api
q.push(function(cb) {
results.push('two');
cb();
});
q.push(
function(cb) {
results.push('four');
cb();
},
function(cb) {
results.push('five');
cb();
}
);
q.unshift(function(cb) {
results.push('one');
cb();
});
q.splice(2, 0, function(cb) {
results.push('three');
cb();
});
// use the timeout feature to deal with jobs that
// take too long or forget to execute a callback
q.on('timeout', function(job, next) {
console.log('job timed out:', job.toString().replace(/\n/g, ''));
next();
})
q.push(function(cb) {
setTimeout(function() {
console.log('slow job finished');
cb();
}, 200);
});
q.push(function(cb) {
console.log('forgot to execute callback');
});
Version 1.0 introduces api changes and is NOT backwards compatible with 0.0.2
FAQs
asynchronous function queue with adjustable concurrency
The npm package queue receives a total of 1,936,232 weekly downloads. As such, queue popularity was classified as popular.
We found that 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.