promise queue javascript
// promise queue
const Queue = require('promise-queue');
var maxConcurrent = 1;
var maxQueue = Infinity;
var queue = new Queue(maxConcurrent, maxQueue);
(async () => {
const promises = [];
// logic here
function getDownloader() {
return async () => {
await new Promise((res, rej) => {
setTimeout(() => {
console.log("hello data");
res();
}, 3000);
})
};
}
for (let i = 0; i < 4; i++) {
promises.push(queue.add(getDownloader()))
}
const start = Date.now()
await Promise.all(promises)
console.log(Date.now() - start)
})()