p-limit
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
yocto-queue | 0.1.0 | 2.7 kB | MIT | prod |
p-limit is a powerful utility package in npm that principally helps in executing multiple promise-returning and asynchronous functions with limited concurrency. Effectively, it allows you to limit the number of promises or asynchronous functions running concurrently, aiding in performance control and process management.
To use p-limit, first, install the npm package using npm install p-limit
. Next, you import the package into your project using import pLimit from 'p-limit';
. Once imported, you can initiate p-limit with the desired concurrency limit, for example, const limit = pLimit(1);
. Then, you wrap the asynchronous functions or promises you want to limit within the limit
function. Here's a basic usage example:
import pLimit from 'p-limit';
// Initialize p-limit with a concurrency limit of 1
const limit = pLimit(1);
const input = [
// Wrap the async functions within the limit function
limit(() => fetchSomething('foo')),
limit(() => fetchSomething('bar')),
limit(() => doSomething())
];
// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);
In this example, even if the fetchSomething
and doSomething
functions are asynchronous, they will be executed one by one because pLimit
is configured with a concurrency limit of 1.
The documentation for p-limit, including its usage details and API reference, is contained within its GitHub readme at https://github.com/sindresorhus/p-limit. The documentation provides detailed descriptions of its API functionalities such as pLimit(concurrency)
, limit(fn, ...args)
, limit.activeCount
, limit.pendingCount
, and limit.clearQueue()
. In addition to that, it explains what p-limit is, how it varies from the p-queue package, and how to install and use the package efficaciously.