npm install array-async-queue
Asynchronously go through an array with concurrency and queue
const arrayAsyncQueue = require("array-async-queue");
const arr = [];
const concurrency = 10;
for (let i = 0; i < 1000; i++) {
arr.push(i);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
arrayAsyncQueue(arr, concurrency, (val, stop) => {
console.log(val);
if(getRandomInt(0, 100) >= 99) {
// stop the queue if necessary
return stop();
}
return new Promise(resolve => setTimeout(resolve, getRandomInt(0, 50))
})
Initially, the functions will be launched in parallel in the quantity specified in the variable concurrency. After completing one of them, the next one remaining in the array will be called. At the same time, the number of parallel called functions is always equal to concurrency.