p-limit vs p-map
Choosing collection mapping or a shared concurrency limit
p-limitp-mapSimilar Packages:

Choosing collection mapping or a shared concurrency limit

Compare p-map and p-limit by how work is submitted, how results are consumed and who owns queue/error handling.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
p-limit469,528,1732,94316.1 kB02 months agoMIT
p-map104,924,0581,52167.9 kB0a month agoMIT

Collection transformation or a shared budget

p-map and p-limit both control asynchronous work, but expose different ownership models. Neither is a requests-per-second limiter or a worker-thread engine. Start work inside the submitted function or mapper; wrapping an already-started operation cannot limit its start.

Submission and result contracts

p-limit creates a limiter used to schedule functions. The application decides how to combine the returned promises and which call sites share it. activeCount, pendingCount and configurable concurrency expose scheduling state. Its map helper does not turn the limiter into a comprehensive queue manager. The package is ESM with Node >=20 declared for Node use.

p-map receives an iterable and mapper, with explicit concurrency and an array result in input order. Its default concurrency is unbounded. It suits processing a finite collection where the operation and result relationship should be expressed together. The package is ESM with Node >=18.

For progressive consumption, pMapIterable is a separate async-iterable interface with backpressure. By default it preserves input order; its preserveOrder option can yield ready results sooner when order is unnecessary. That changes when results and errors become visible and is not the array-returning API's contract.

Failure and shutdown

With p-limit, clearQueue discards waiting work and does not cancel running operations. Configure rejectOnClear when callers must receive rejection for discarded pending calls; otherwise awaiting those calls can remain unresolved. Active-operation cancellation needs cooperation from that operation.

With p-map, choose the documented mapper error/abort policy and distinguish stopping new scheduling from aborting work already started. The iterable source also has a lifetime: an input blocked in next needs cancellation at that source. A mapper's rejected result is not proof that every external operation stopped.

Use p-map when a collection and its transformed results are the core abstraction. Use p-limit when multiple independent function calls need the same cap. Evaluate a dedicated queue/rate interface only when that lifecycle or timing policy is actually required, and test representative error paths instead of assuming similarly named concurrency settings make the libraries interchangeable.

How to Choose: p-limit vs p-map

  • p-limit:

    Choose shared function scheduling when unrelated operations should draw from one concurrency budget.

  • p-map:

    Choose a mapper over inputs when ordered results or progressive async-iterable consumption are the main task.

README for p-limit

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) default export

Returns a limit function.

concurrency

Type: number | object
Minimum: 1

Concurrency limit.

You can pass a number or an options object with a concurrency property.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

import pLimit from 'p-limit';

const limit = pLimit({concurrency: 1});

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.

Warning: Avoid calling the same limit function inside a function that is already limited by it. This can create a deadlock where inner tasks never run. Use a separate limiter for inner tasks.

limit.map(iterable, mapperFunction)

Process an iterable of inputs with limited concurrency.

The mapper function receives the item value and its index.

Returns a promise equivalent to Promise.all(Array.from(iterable, (item, index) => limit(mapperFunction, item, index))).

If the iterable throws during iteration, the returned promise rejects with that error. Mapper functions that were already scheduled still run, and their results are discarded.

This is a convenience function for processing inputs that arrive in batches. For more complex use cases, see p-map.

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 tear down 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.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

limit.concurrency

Get or set the concurrency limit.

limitFunction(fn, options) named export

Returns a function with limited concurrency.

The returned function manages its own concurrent executions, allowing you to call it multiple times without exceeding the specified concurrency limit.

Ideal for scenarios where you need to control the number of simultaneous executions of a single function, rather than managing concurrency across multiple functions.

import {limitFunction} from 'p-limit';

const limitedFunction = limitFunction(async () => {
	return doSomething();
}, {concurrency: 1});

const input = Array.from({length: 10}, limitedFunction);

// Only one promise is run at once.
await Promise.all(input);

fn

Type: Function

Promise-returning/async function.

options

Type: object

concurrency

Type: number
Minimum: 1

Concurrency limit.

rejectOnClear

Type: boolean
Default: false

Reject pending promises with an AbortError when clearQueue() is called. This is recommended if you await the returned promises, for example with Promise.all, so pending tasks do not remain unresolved after clearQueue().

limitedFunction.clearQueue()

Discard pending calls that have not started yet.

When rejectOnClear is enabled, pending promises are rejected with an AbortError. Otherwise, their promises remain unresolved.

This does not cancel calls that are already running.

Recipes

See recipes.md for common use cases and patterns.

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-map - Run promise-returning & async functions concurrently with different inputs
  • p-all - Run promise-returning & async functions concurrently with optional limited concurrency
  • More…