What is queue?
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.
What are queue's main functionalities?
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();
Other packages similar to queue
async
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
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
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.
____ __ _____ __ _____
/ __ `/ / / / _ \/ / / / _ \
/ /_/ / /_/ / __/ /_/ / __/
\__, /\__,_/\___/\__,_/\___/
/_/
asynchronous function queue with adjustable concurrency.
why
async is a big libray offering various approaches to dealing with asynchrony; queue
is a small library offering a single, flexible abstraction.
how
this module exports a class Queue
that implements most of the Array
api. pass async functions (ones that accept a callback) to an instance's additive array methods. processing begins when you call q.start()
install
npm install queue
test
npm test
npm run test-browser
example
npm run example
var queue = require('queue');
var q = queue();
var results = [];
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();
});
q.timeout = 100;
q.on('timeout', function(next, job) {
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');
});
q.on('success', function(result, job) {
console.log('job finished processing:', job.toString().replace(/\n/g, ''));
});
q.start(function(err) {
console.log('all done:', results);
});
require
var queue = require('queue')
constructor
var q = queue([opts])
where opts
may contain inital values for:
instance methods
q.start([cb])
cb, if passed will be called when the queue empties
q.stop()
stops the queue. can be resumed with q.start()
q.end([err])
stop and empty the queue immediately
instance methods mixed in from Array
Mozilla has docs on how these methods work here.
q.push(element1, ..., elementN)
q.unshift(element1, ..., elementN)
q.splice(index , howMany[, element1[, ...[, elementN]]])
q.pop()
q.shift()
q.slice(begin[, end])
q.reverse()
q.indexOf(searchElement[, fromIndex])
q.lastIndexOf(searchElement[, fromIndex])
properties
q.concurrency
max number of jobs the queue should process concurrently, defaults to Infinity
q.timeout
milliseconds to wait for a job to execute its callback
q.length
jobs pending + jobs to process (readonly)
events
q.emit('success', result, job)
after a job executes its callback
q.emit('error', err, job)
after a job passes an error to its callback
q.emit('timeout', continue, job)
after q.timeout
milliseconds have elapsed and a job has not executed its callback
q.emit('end'[, err])
after all jobs have been processed
changelog
- version 3.0
- changes the default concurrency to
Infinity
- allow
q.start()
to accept an optional callback executed on q.emit('end')
- version 2.0 introduces api changes and is not backwards compatible with 1.0
license
WTFPL