github-pages-deploy-action/node_modules/p-limit/index.js

43 lines
633 B
JavaScript
Raw Normal View History

2020-01-28 13:07:56 +08:00
'use strict';
const pTry = require('p-try');
2020-03-22 05:13:25 +08:00
module.exports = concurrency => {
if (concurrency < 1) {
throw new TypeError('Expected `concurrency` to be a number from 1 and up');
2020-01-28 13:07:56 +08:00
}
const queue = [];
let activeCount = 0;
const next = () => {
activeCount--;
if (queue.length > 0) {
queue.shift()();
}
};
2020-03-22 05:13:25 +08:00
return fn => new Promise((resolve, reject) => {
const run = () => {
activeCount++;
pTry(fn).then(
val => {
resolve(val);
next();
},
err => {
reject(err);
next();
}
);
};
2020-01-28 13:07:56 +08:00
if (activeCount < concurrency) {
2020-03-22 05:13:25 +08:00
run();
2020-01-28 13:07:56 +08:00
} else {
2020-03-22 05:13:25 +08:00
queue.push(run);
2020-01-28 13:07:56 +08:00
}
});
};