I have this vanilla Node.js code:
const http = require('http');
const host = 'example.com';
const path = '/';
let i = 0;
const run = () => {
console.log(i++);
return new Promise(resolve => {
const req = http.request({host, path}, res => {
res.pipe(process.stdout);
res.once('end', resolve);
});
req.end();
});
};
async function doall() {
for (let i = 0; i < 50; i++) {
await Promise.all(new Array(10).fill(null).map(run));
}
}
const now = Date.now();
console.log('Starting up.');
doall().then(_ => {
console.log('done after:', Date.now() - now, 'millis');
});
// the end
this works - it runs 50 sets of 10... except the problem is that, all 10 complete, then the next 10 start, then the next 10 complete. So there are moments when 0 requests are in progress, between each set.
Is there some way using vanilla Node.js and promises to replicate async.eachLimit(new Array(500), 20, ...)
?