RepoPilot

What types does the main pLimit function expect for its concurrency argument?

About sindresorhus/p-limit · 2 messages · last activity 8/1/2026

Question

What types does the main pLimit function expect for its concurrency argument?

RepoPilot

The concurrency argument accepts either a number or an object (an options object with a concurrency property), as documented in both the README and the TypeScript declarations.

Runtime behavior [index.js:L3-L10]:

export default function pLimit(concurrency) {
	let rejectOnClear = false;

	if (typeof concurrency === 'object') {
		({concurrency, rejectOnClear = false} = concurrency);
	}

	validateConcurrency(concurrency);
	...

If an object is passed, it destructures concurrency and an optional rejectOnClear (defaulting to false) out of it [index.js:L6-L9].

Validation happens via validateConcurrency [index.js:L123-L128]:

function validateConcurrency(concurrency) {
	if (!((Number.isInteger(concurrency) || concurrency === Number.POSITIVE_INFINITY) && concurrency > 0)) {
		throw new TypeError('Expected `concurrency` to be a number from 1 and up');
	}
}

So the resolved concurrency value must be either:

  • A positive integer (minimum 1), or
  • Number.POSITIVE_INFINITY

Anything else (non-integer numbers, 0, negative numbers, undefined, booleans, or an empty object without a valid concurrency property) throws a TypeError. This is confirmed in the tests [test.js:L275-L296], which show pLimit(0), pLimit(-1), pLimit(1.2), pLimit(undefined), pLimit(true), and pLimit({}) all throwing.

Type declarations confirm this: the Options type declares concurrency as readonly number with minimum 1 [index.d.ts:L91-L98], and the pLimit doc comment states "Concurrency limit. Minimum: 1. You can pass a number or an options object with a concurrency property" [index.d.ts:L59-L60]. The README also documents concurrency as number | object [readme.md:L38-L44].

Additionally, concurrency can also be reassigned later via the concurrency setter on the returned limit function, which re-validates using the same validateConcurrency check [index.js:L92-L102].

FOLLOW_UPS:

  • How does the concurrency setter reprocess the queue?
  • What happens when clearQueue() is called with rejectOnClear?
  • How does limit.map differ from calling limit directly?

Want to ask your own question?

Open chat for sindresorhus/p-limit

Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/p-limit. AI answers can be incomplete or stale — verify before relying on them.