I've a very large amount of data to create on the database and I want to generate them in parallel with a concurrency limit. In order to do that I'm using the Promise.map method from bluebird with the concurrency limit set. However, I also need to stop the execution of the chain as soon as one of the promises fails.
This is my current code:
await Promise.map(
entry.tags,
async (tag) => {
await factory(Tag)().create(tag);
},
{ concurrency: 1000 },
).catch(() => {
/** @todo find a better way */
process.exit(1);
});
At the moment, as you can see, I'm using process.exit(1) but I wouldn't handle the exit code there. Instead, the Promise chain should stop instantly and the error should be thrown in order to be caught from the parent function.
With the p-map library there's the parameter "stopOnError" which work exactly as I expect, I can't find anything similar on Bluebird instead.
I tried also to use the .cancel() method inside the .catch() but apparently it's not working.