My question is basically a combination of
I'm aware of Promise.allSettled
, but I'm failing to find a good way to also limit concurrency.
What I have so far:
Idea 1 using p-limit
:
const pLimit = require('p-limit');
const limit = pLimit(10);
let promises = files.map(pair => {
var formData = {
'file1': fs.createReadStream(pair[0]),
'file2': fs.createReadStream(pair[1])
};
return limit(() => uploadForm(formData));
});
(async () => {
const result = await Promise.allSettled(promises).then(body => {
body.forEach(value => {
if(value.status == "rejected")
file.write(value.reason + '\n---\n');
});
});
})();
My problem with this solution is, that I have to create all promises in the first place and in doing so opening two file streams for each promise and I'll hit the limit of open files.
Idea 2 using p-queue
:
I tried around with generator functions to create and add new promises in the queue.on
'next'
event, but I couldn't get it to work properly and this is probably not the right tool for this job.
Idea 3 using a PromisePool:
This looked very promising in the beginning. Some of them support a generator function to create the promises for the pool, but I couldn't find one, who explicitly stated to behave like Promise.allSettled
.
I implemented es6-promise-pool
only to find out that it will stop after the first promise rejection.