3 ms·
How would you use reduce for that?
by polishdude20 3y ago
How would you use reduce for that?
- roblh 3y agoReduce can use an async function which wraps the collector in a promise and gives you a ton of control over how it executes because you can choose at what point you want the function to block waiting for previous results. In this particular case it's a lot more verbose, obviously, but if you're doing any kind of further processing of each element, you can move it into the reduce and be able to control exactly what you get out of it if something fails for any reason, while still running mostly concurrently like all or allSettled would. const result = await elements.reduce(async (collector, x) => { const arg3 = computeArg3(x) // explicitly handle whatever errors, try catch, whatever you need const asyncResult = await someAsyncFunction3(arg3) // this has to happen after calling someAsyncFunction because it will block until the previous iteration finishes collector = await collector; collector.push(result) return collector }, []);