p-queue
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
eventemitter3 | 5.0.1 | 11.81 kB | MIT | prod | |
p-timeout | 5.1.0 | 3.55 kB | MIT | prod |
P-Queue is a popular NPM package that serves as a promise queue with concurrency control. It's predominantly beneficial for rate-limiting async (or synchronous) operations, such as when interacting with a REST API or undertaking CPU/memory intensive tasks. If you're running a server, you might want to consider a Redis-backed job queue instead.
To use P-Queue, you first need to install it using npm with the command npm install p-queue
. Here's a basic coding example demonstrating how P-Queue is typically used:
import PQueue from 'p-queue';
import got from 'got';
const queue = new PQueue({concurrency: 1});
(async () => {
await queue.add(() => got('https://sindresorhus.com'));
console.log('Done: sindresorhus.com');
})();
(async () => {
await queue.add(() => got('https://avajs.dev'));
console.log('Done: avajs.dev');
})();
(async () => {
const task = await getUnicornTask();
await queue.add(task);
console.log('Done: Unicorn task');
})();
In this example, only one promise is run at a time. If you want to run more than one promise concurrently, you would just increase the concurrency level when you initialize a new P-Queue.
When it comes to documentation, the most comprehensive resource for understanding and using P-Queue is located right in the README file on the package's GitHub repo at https://github.com/sindresorhus/p-queue. Here, you'll find detailed installation instructions, a succinct explanation of what the package does, usage instructions, an extensive API reference, event details, advanced examples, FAQs, related packages, and a list of maintainers. So, if you're looking to get up-to-speed on P-Queue fast, the GitHub repo is the best place to start.