🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

queue

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

queue

asynchronous function queue with adjustable concurrency

7.0.0
latest
Version published
Weekly downloads
6.2M
2.58%
Maintainers
1
Weekly downloads
 
Created

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

FAQs

Package last updated on 11 Apr 2023

Did you know?

Socket

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.

Install

Related posts