promise-call-limit
Advanced tools
Comparing version 1.0.2 to 2.0.0
29
index.js
@@ -8,6 +8,6 @@ const os = require('os') | ||
const defLimit = 'availableParallelism' in os | ||
? os.availableParallelism() | ||
: Math.max(1, os.cpus().length) | ||
? Math.max(1, os.availableParallelism() - 1) | ||
: Math.max(1, os.cpus().length - 1) | ||
const callLimit = (queue, limit = defLimit) => new Promise((res, rej) => { | ||
const callLimit = (queue, { limit = defLimit, rejectLate } = {}) => new Promise((res, rej) => { | ||
let active = 0 | ||
@@ -17,3 +17,5 @@ let current = 0 | ||
// Whether or not we rejected, distinct from the rejection just in case the rejection itself is falsey | ||
let rejected = false | ||
let rejection | ||
const reject = er => { | ||
@@ -23,3 +25,5 @@ if (rejected) | ||
rejected = true | ||
rej(er) | ||
rejection = er | ||
if (!rejectLate) | ||
rej(rejection) | ||
} | ||
@@ -37,5 +41,4 @@ | ||
const c = current++ | ||
if (c >= queue.length) { | ||
return resolve() | ||
} | ||
if (c >= queue.length) | ||
return rejected ? reject() : resolve() | ||
@@ -46,12 +49,18 @@ active ++ | ||
results[c] = result | ||
return result | ||
}, (er) => { | ||
active -- | ||
reject(er) | ||
}).then(result => { | ||
if (rejected && active === 0) | ||
return rej(rejection) | ||
run() | ||
return result | ||
}, reject) | ||
}) | ||
} | ||
for (let i = 0; i < limit; i++) { | ||
for (let i = 0; i < limit; i++) | ||
run() | ||
} | ||
}) | ||
module.exports = callLimit |
{ | ||
"name": "promise-call-limit", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"files": [ | ||
@@ -5,0 +5,0 @@ "index.js" |
@@ -13,3 +13,4 @@ # promise-call-limit | ||
// frobulate no more than 4 things in parallel | ||
promiseCallLimit(things.map(thing => () => frobulateThing(thing)), 4) | ||
promiseCallLimit(things.map(thing => () => frobulateThing(thing)), { | ||
limit: 4 }) | ||
.then(results => console.log('frobulated 4 at a time', results)) | ||
@@ -20,9 +21,14 @@ ``` | ||
### promiseCallLimit(queue Array<() => Promise>, limit = defaultLimit) | ||
### promiseCallLimit(queue Array<() => Promise>, opts<Object>) | ||
The default limit is the number of CPUs on the system - 1, or 1. | ||
opts can contain: | ||
- limit: specified concurrency limit. Defaults to the number of | ||
CPUs on the system minus one. Presumably the main thread is taking | ||
up a CPU as well, so let's not be greedy. In the case where there | ||
is only one cpu the limit will default to 1. | ||
- rejectLate: if true, then any rejection will not prevent the rest of | ||
the queue from running. Any subsequent rejections will be ignored, | ||
and the first rejection will be what the function finally rejects | ||
with. | ||
The reason for subtracting one is that presumably the main thread is taking | ||
up a CPU as well, so let's not be greedy. | ||
Note that the array should be a list of Promise-_returning_ functions, not | ||
@@ -29,0 +35,0 @@ Promises themselves. If you have a bunch of Promises already, you're best |
4163
53
37