Skip to content

Commit

Permalink
fix: revert pMap.concurrency back to Infinity
Browse files Browse the repository at this point in the history
Promise.all has advantage of keeping async stack traces,
this is often more important than throttling "loose" promises.
  • Loading branch information
kirillgroshkov committed Apr 5, 2024
1 parent 194be8d commit 5d2f532
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/promise.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Based on [p-map](https://github.com/sindresorhus/p-map)

Allows to asynchronously map an array of Promises, with options to:

- control `concurrency` (default: `16`)
- control `concurrency` (default: `Infinity`)
- control error behavior (`ErrorMode`):
- `THROW_IMMEDIATELY` (default)
- `THROW_AGGREGATED`: throw `AggregateError` in the end of execution, if at least 1 error happened
Expand Down
12 changes: 4 additions & 8 deletions src/promise/pMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ export interface PMapOptions {
/**
* Number of concurrently pending promises returned by `mapper`.
*
* Defaults to 16.
*
* It previously (and originally) defaulted to Infinity, which was later changed,
* because it's somewhat dangerous to run "infinite number of parallel promises".
* You can still emulate the old behavior by passing `Infinity`.
* Defaults to Infitity.
*/
concurrency?: number

Expand Down Expand Up @@ -73,16 +69,16 @@ export async function pMap<IN, OUT>(
const itemsLength = items.length
if (itemsLength === 0) return [] // short circuit

const { concurrency = 16, errorMode = ErrorMode.THROW_IMMEDIATELY, logger = console } = opt
const { concurrency = Infinity, errorMode = ErrorMode.THROW_IMMEDIATELY, logger = console } = opt

// Special cases that are able to preserve async stack traces
// Special case: serial execution
if (concurrency === 1) {
return await pMap1(items, mapper, errorMode, logger)
}

// Special case: concurrency === Infinity or items.length <= concurrency
if (concurrency === Infinity || items.length <= concurrency) {
// Special case: items.length <= concurrency (including when concurrency === Infinity)
if (items.length <= concurrency) {
return await pMapAll(items, mapper, errorMode, logger)
}

Expand Down

0 comments on commit 5d2f532

Please sign in to comment.