Convert Figma logo to code with AI

sindresorhus logop-limit

Run multiple promise-returning & async functions with limited concurrency

1,929
101
1,929
7

Top Related Projects

1,331

Cancelable Async Flows (CAF)

Job scheduler and rate limiter, supports Clustering

Quick Overview

p-limit is a Node.js library that provides a function to limit the number of concurrent promises that can be executed at a time. This can be useful when you have a large number of asynchronous operations that need to be executed, but you want to limit the number of concurrent operations to avoid overloading the system.

Pros

  • Concurrency Control: p-limit allows you to control the number of concurrent promises that can be executed, which can be useful for managing system resources and avoiding overloading.
  • Simple API: The library has a straightforward and easy-to-use API, making it simple to integrate into your project.
  • Flexible: p-limit can be used with any kind of promise-returning function, not just specific to a particular library or framework.
  • Lightweight: The library is small and lightweight, with minimal dependencies, making it easy to include in your project.

Cons

  • Limited Functionality: p-limit is a relatively simple library, and it may not provide all the features or customization options that some users might need.
  • Potential Performance Impact: Limiting the number of concurrent promises can have a performance impact, especially if the promises are quick to resolve. This may need to be considered when using the library.
  • Lack of Error Handling: The library does not provide any built-in error handling or error reporting, which may require additional code to be written.
  • Dependency on Promises: p-limit is dependent on the use of Promises, which may not be suitable for all use cases or older environments.

Code Examples

Here are a few examples of how to use p-limit:

const pLimit = require('p-limit');

// Create a limiter that allows a maximum of 2 concurrent promises
const limit = pLimit(2);

// Execute 5 asynchronous operations with the limiter
Promise.all([
  limit(() => fetchData(1)),
  limit(() => fetchData(2)),
  limit(() => fetchData(3)),
  limit(() => fetchData(4)),
  limit(() => fetchData(5))
]).then(results => {
  console.log(results);
});

function fetchData(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(`Data for item ${id}`);
    }, Math.random() * 1000);
  });
}

In this example, we create a pLimit instance that allows a maximum of 2 concurrent promises. We then use the limit function to execute 5 asynchronous operations, ensuring that no more than 2 are running at the same time.

const pLimit = require('p-limit');

// Create a limiter that allows a maximum of 5 concurrent promises
const limit = pLimit(5);

// Execute a series of asynchronous operations with the limiter
const promises = [];
for (let i = 0; i < 10; i++) {
  promises.push(limit(() => fetchData(i)));
}

Promise.all(promises).then(results => {
  console.log(results);
});

function fetchData(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(`Data for item ${id}`);
    }, Math.random() * 1000);
  });
}

In this example, we create a pLimit instance that allows a maximum of 5 concurrent promises. We then execute 10 asynchronous operations, using the limit function to ensure that no more than 5 are running at the same time.

const pLimit = require('p-limit');

// Create a limiter that allows a maximum of 3 concurrent promises
const limit = pLimit(3);

// Execute a series of asynchronous operations with the limiter
const promises = [];
for (let i = 0; i < 7; i++) {
  promises.push(limit(() => fetchData(i)));
}

Promise.all(promises).then(results => {
  console.log(results);
});

function fetchData(id) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(`Data for item ${id}`);
    }, Math.random() * 1000);
  });
}

Competitor Comparisons

1,331

Cancelable Async Flows (CAF)

Pros of CAF

  • Provides a more comprehensive approach to managing asynchronous operations with cancellation and timeouts
  • Offers a cleaner syntax for handling complex async flows
  • Allows for better resource management through automatic cancellation of child tasks

Cons of CAF

  • Has a steeper learning curve due to its more complex API
  • May be overkill for simple concurrency control scenarios
  • Less widely adopted compared to p-limit

Code Comparison

p-limit:

const pLimit = require('p-limit');

const limit = pLimit(2);
const input = [1, 2, 3, 4, 5];

Promise.all(input.map(i => limit(() => fetchData(i))));

CAF:

const CAF = require('caf');

const cancelToken = CAF.cancelToken();
const limited = CAF(function* (signal, ...args) {
    // Async operations with cancellation support
});

limited(cancelToken, ...args);

Summary

While p-limit focuses on simple concurrency control, CAF offers a more robust solution for managing complex asynchronous operations with built-in cancellation support. p-limit is easier to use for basic scenarios, but CAF provides more advanced features for handling intricate async flows and resource management. The choice between the two depends on the specific requirements of your project and the level of control you need over asynchronous operations.

Job scheduler and rate limiter, supports Clustering

Pros of Bottleneck

  • More advanced features like clustering and Redis support
  • Flexible configuration options for different use cases
  • Built-in support for prioritization and weight-based throttling

Cons of Bottleneck

  • Larger package size and more dependencies
  • Steeper learning curve due to additional complexity
  • May be overkill for simple concurrency limiting needs

Code Comparison

p-limit:

import pLimit from 'p-limit';

const limit = pLimit(1);
const input = [1, 2, 3, 4, 5];
const promises = input.map(i => limit(() => fetchSomething(i)));

Bottleneck:

import Bottleneck from 'bottleneck';

const limiter = new Bottleneck({maxConcurrent: 1, minTime: 333});
const input = [1, 2, 3, 4, 5];
const promises = input.map(i => limiter.schedule(() => fetchSomething(i)));

Both libraries provide concurrency control, but Bottleneck offers more advanced features and configuration options. p-limit is simpler and more lightweight, making it easier to use for basic concurrency limiting. Bottleneck is better suited for complex rate limiting scenarios, while p-limit excels in simplicity and ease of use for straightforward concurrency control.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

p-limit

Run multiple promise-returning & async functions with limited concurrency

Works in Node.js and browsers.

Install

npm install p-limit

Usage

import pLimit from 'p-limit';

const limit = pLimit(1);

const input = [
	limit(() => fetchSomething('foo')),
	limit(() => fetchSomething('bar')),
	limit(() => doSomething())
];

// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);

API

pLimit(concurrency)

Returns a limit function.

concurrency

Type: number
Minimum: 1
Default: Infinity

Concurrency limit.

limit(fn, ...args)

Returns the promise returned by calling fn(...args).

fn

Type: Function

Promise-returning/async function.

args

Any arguments to pass through to fn.

Support for passing arguments on to the fn is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.

limit.activeCount

The number of promises that are currently running.

limit.pendingCount

The number of promises that are waiting to run (i.e. their internal fn was not called yet).

limit.clearQueue()

Discard pending promises that are waiting to run.

This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.

Note: This does not cancel promises that are already running.

limit.concurrency

Get or set the concurrency limit.

FAQ

How is this different from the p-queue package?

This package is only about limiting the number of concurrent executions, while p-queue is a fully featured queue implementation with lots of different options, introspection, and ability to pause the queue.

Related

  • p-throttle - Throttle promise-returning & async functions
  • p-debounce - Debounce promise-returning & async functions
  • p-all - Run promise-returning & async functions concurrently with optional limited concurrency
  • More…

NPM DownloadsLast 30 Days