What types does the main pLimit function expect for its concurrency argument?
What types does the main pLimit function expect for its concurrency argument?
About sindresorhus/p-limit · 2 messages · last activity 7/27/2026
The pLimit function accepts a concurrency parameter typed as number | Options [index.d.ts:L89-L90].
Breaking this down:
number— a plain concurrency limit, minimum1[index.d.ts:L89-L90], [readme.md:L33-L45].Options— an object with:concurrency: number(required, minimum1) [index.d.ts:L92-L97]rejectOnClear?: boolean(optional, defaultfalse) — rejects pending promises with anAbortErrorwhenclearQueue()is called [index.d.ts:L99-L106].
At runtime, index.js implements this union manually: if typeof concurrency === 'object', it destructures {concurrency, rejectOnClear = false} from it [index.js:L3-L11]. It then validates the resolved concurrency value via validateConcurrency, which throws a TypeError unless the value is a positive integer or Number.POSITIVE_INFINITY [index.js:L123-L128], [index.js:L9]. It also throws a TypeError if rejectOnClear isn't a boolean [index.js:L12-L14].
This matches the test suite, which confirms pLimit throws on 0, -1, 1.2, undefined, true, {}, and {concurrency: 0} [test.js:L275-L303].
Note that Number.POSITIVE_INFINITY is valid at runtime per validateConcurrency [index.js:L124], even though the TypeScript type just says number — infinity is a legitimate number value in JS/TS, so this is consistent with the type but worth knowing as an effectively "unlimited concurrency" option.
FOLLOW_UPS:
- How does
validateConcurrencydistinguish valid numbers from invalid ones? - What happens internally when
limit.concurrencyis changed at runtime? - How does
rejectOnClearaffect pending promises inclearQueue()?
Want to ask your own question?
Open chat for sindresorhus/p-limitPublic 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.