0

Have run into a strange problem that I've never had before making async calls inside a reduce function,

here is an example

const days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']

const reduced = await days.reduce(async (acc, e) => {
   // some async call to an api
   const rosteredPerson = await getPerson()

   acc[e] = rosteredPerson
   return acc
}, {})

Now I would expect the output to look something like:

[{
   'monday': 'tom',
   'tuesday': 'james',
   'wednesday': 'kate',
   etc...
}]

but its behaving unexpectedly due to the async calls and its end up returning with a single item:

[{
   'monday': 'tom'
}]

Am I missing something when it comes to using async/await inside a reduce function... Any help would be appreciated

SkinnyBetas
  • 461
  • 1
  • 5
  • 27

0 Answers0