I’ve been giving some thought at what could be the best way to deal with a batch of heavy operations with Javascript and I came up with the following:
const results: Promise<void>[] = [];
// Start all pieces of work concurrently
for (const record of await getData()) {
results.push(doSomeHeavyWork(records));
}
// Collect the results
for (const result of results) {
// Isolate the failures so one doesn't break another.
try {
await result;
} catch (error) {
console.log(JSON.stringify(error));
}
}
My understanding is that the above snippet takes as long as the longest operation, and that’s as good as it’s going to get AFAIK. However, is there a better or more idiomatic way of going about this problem?
I’m not really looking necessarily at node here. This could be node, Deno or browser code.