feat(async-each): concurrency 0 means no limit

It's identical to `Infinity` but has broader support (e.g. in JSON).
This commit is contained in:
Julien Fontanet 2022-07-04 17:19:19 +02:00
parent 34c849ee89
commit a6fee2946a
4 changed files with 6 additions and 2 deletions

View File

@ -14,7 +14,7 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
`opts` is an object that can contains the following options:
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`. The value `0` means no concurrency limit.
- `signal`: an abort signal to stop the iteration
- `stopOnError`: wether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`

View File

@ -32,7 +32,7 @@ Returns a promise wich rejects as soon as a call to `iteratee` throws or a promi
`opts` is an object that can contains the following options:
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`
- `concurrency`: a number which indicates the maximum number of parallel call to `iteratee`, defaults to `1`. The value `0` means no concurrency limit.
- `signal`: an abort signal to stop the iteration
- `stopOnError`: wether to stop iteration of first error, or wait for all calls to finish and throw an `AggregateError`, defaults to `true`

View File

@ -10,6 +10,9 @@ class AggregateError extends Error {
}
exports.asyncEach = function asyncEach(iterable, iteratee, { concurrency = 1, signal, stopOnError = true } = {}) {
if (concurrency === 0) {
concurrency = Infinity
}
return new Promise((resolve, reject) => {
const it = (iterable[Symbol.iterator] || iterable[Symbol.asyncIterator]).call(iterable)
const errors = []

View File

@ -29,6 +29,7 @@
<!--packages-start-->
- @vates/async-each minor
- xo-web minor
<!--packages-end-->