JavaScript array .reduce with async/await

The problem is that your accumulator values are promises – they’re return values of async functions. To get sequential evaluation (and all but the last iteration to be awaited at all), you need to use

const data = await array.reduce(async (accumP, current, index) => {
  const accum = await accumP;
  …
}, Promise.resolve(…));

That said, for async/await I would in general recommend to use plain loops instead of array iteration methods, they’re more performant and often simpler.

Leave a Comment