Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

p-queue

Package Overview
Dependencies
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-queue

Promise queue with concurrency control

  • 8.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.9M
increased by0.25%
Maintainers
2
Weekly downloads
 
Created

What is p-queue?

The p-queue package is a promise-based queue for Node.js that enables the execution of tasks in a controlled concurrency environment. It allows for rate-limiting, pausing, and resuming of tasks, and ensures that tasks are executed in a predictable manner.

What are p-queue's main functionalities?

Concurrency Control

This feature allows you to control the number of tasks that are run concurrently. In this example, the concurrency is set to 2, meaning that only two tasks will be processed at the same time.

const PQueue = require('p-queue').default;
const queue = new PQueue({concurrency: 2});

async function task(input) {
  // Task implementation
}

queue.add(() => task('task 1'));
queue.add(() => task('task 2'));
queue.add(() => task('task 3'));

Rate Limiting

This feature allows you to limit the rate at which tasks are executed. In this example, the queue is configured to process a maximum of 2 tasks per 1000 milliseconds.

const PQueue = require('p-queue').default;
const queue = new PQueue({
  interval: 1000,
  intervalCap: 2
});

async function task(input) {
  // Task implementation
}

for (let i = 0; i < 6; i++) {
  queue.add(() => task(`task ${i + 1}`));
}

Pausing and Resuming

This feature allows you to pause the processing of tasks and resume it later. In this example, the queue is paused immediately after creation, tasks are added, and then the queue is resumed after a timeout.

const PQueue = require('p-queue').default;
const queue = new PQueue();

queue.pause();

async function task(input) {
  // Task implementation
}

queue.add(() => task('task 1'));
queue.add(() => task('task 2'));

setTimeout(() => {
  queue.start(); // Resume the queue after 2000ms
}, 2000);

Priority Queueing

This feature allows you to add tasks with a priority level. Tasks with a lower priority number are executed first. In this example, the 'high priority' task will be executed before the 'low priority' task.

const PQueue = require('p-queue').default;
const queue = new PQueue();

async function task(input) {
  // Task implementation
}

queue.add(() => task('low priority'), {priority: 1});
queue.add(() => task('high priority'), {priority: 0});

Other packages similar to p-queue

Keywords

FAQs

Package last updated on 14 Dec 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc