'Custom for..of using queue to solve concurrency in promises yields slower requests than Promise.all
here is my problem: I have a stack of x promises that I want to solve 5 at a time (basically what Plimit does)
Here is the code that I implemented using for...of
async queuefy(items, concurrency, callback) {
console.log(items.length);
let queue = [];
let singleQueue = [];
let now = 0;
items.forEach((item, index) => {
now++;
singleQueue.push(item);
if(now === concurrency || index === items.length - 1) {
queue.push(singleQueue);
now = 1;
singleQueue = [];
}
});
let batch = 0;
let ret = [];
for(const que of queue) {
const currentRes = await Promise.all(que.map(async (q) => {
return await callback(q);
}));
console.log("Resolved batch: ", ++batch);
ret.push(...currentRes);
}
ret = ret.filter(ret => ret !== undefined);
return ret;
}
Also having havier logic to solve, the time increases from using good ol' Promise.all. Only tested for up to 15 instances of "queuefy".
Is something wrong with my code or will the request time number lower from using Promise.all for larger examples ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|