Hacker News new | past | comments | ask | show | jobs | submit login

How would you use reduce for that?



Reduce 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
  }, []);




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: