What is @types/p-queue?
@types/p-queue provides TypeScript type definitions for the p-queue package, which is a promise queue with concurrency control. It allows you to manage the execution of asynchronous tasks, ensuring that a specified number of tasks run concurrently.
What are @types/p-queue's main functionalities?
Basic Queue Usage
This feature demonstrates how to create a basic queue with a concurrency of 1, meaning tasks will be executed one at a time.
const PQueue = require('p-queue');
const queue = new PQueue({ concurrency: 1 });
queue.add(() => Promise.resolve('Task 1')).then(console.log);
queue.add(() => Promise.resolve('Task 2')).then(console.log);
Concurrency Control
This feature demonstrates how to set a concurrency of 2, allowing two tasks to run concurrently.
const PQueue = require('p-queue');
const queue = new PQueue({ concurrency: 2 });
queue.add(() => new Promise(resolve => setTimeout(() => resolve('Task 1'), 1000))).then(console.log);
queue.add(() => new Promise(resolve => setTimeout(() => resolve('Task 2'), 500))).then(console.log);
queue.add(() => new Promise(resolve => setTimeout(() => resolve('Task 3'), 300))).then(console.log);
Queue Pause and Resume
This feature demonstrates how to pause and resume the queue. Tasks added while the queue is paused will wait until the queue is resumed.
const PQueue = require('p-queue');
const queue = new PQueue({ concurrency: 1 });
queue.add(() => Promise.resolve('Task 1')).then(console.log);
queue.pause();
queue.add(() => Promise.resolve('Task 2')).then(console.log);
queue.add(() => Promise.resolve('Task 3')).then(console.log);
setTimeout(() => queue.start(), 2000);
Other packages similar to @types/p-queue
async
The async package provides various functions for working with asynchronous JavaScript, including queue management. It offers more utilities beyond just queue management, such as parallel execution, series execution, and more.
promise-queue
The promise-queue package is a lightweight promise queue with concurrency control. It is similar to p-queue but with a simpler API and fewer features.
bottleneck
Bottleneck is a powerful rate limiter that also provides queue functionality. It allows you to control the rate of task execution, making it suitable for scenarios where you need to limit the rate of API calls or other operations.