Skip to content
Advertisement

JavaScript array .reduce with async/await

Seem to be having some issues incorporating async/await with .reduce(), like so:

JavaScript

The data object is logged before the this.store completes…

I know you can utilise Promise.all with async loops, but does that apply to .reduce()?

Advertisement

Answer

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

JavaScript

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement