2 const pTry = require('p-try');
4 const pLimit = concurrency => {
5 if (!((Number.isInteger(concurrency) || concurrency === Infinity) && concurrency > 0)) {
6 return Promise.reject(new TypeError('Expected `concurrency` to be a number from 1 and up'));
15 if (queue.length > 0) {
20 const run = (fn, resolve, ...args) => {
23 const result = pTry(fn, ...args);
27 result.then(next, next);
30 const enqueue = (fn, resolve, ...args) => {
31 if (activeCount < concurrency) {
32 run(fn, resolve, ...args);
34 queue.push(run.bind(null, fn, resolve, ...args));
38 const generator = (fn, ...args) => new Promise(resolve => enqueue(fn, resolve, ...args));
39 Object.defineProperties(generator, {
41 get: () => activeCount
44 get: () => queue.length
56 module.exports = pLimit;
57 module.exports.default = pLimit;