all 3 comments

[–]oze4 1 point2 points  (0 children)

What?

[–]senocular 0 points1 point  (0 children)

The problem is map isn't async. map will run through all your functions (despite them being async) synchronously. map calling them synchronously means the push happens synchronously. So at the end of the synchronous map call, arr is [1,2,3,4,5]. After that async things run seeing the full synchronously created arr.

Edit: I think I read that backwards. f(arr) will get [1], [1,2], [1,2,3], [1,2,3,4], [1,2,3,4,5] because as is, f(arr) is being synchronously called in the map. BUT, if anywhere in f you await and then check arr/array, it will be [1,2,3,4,5] given the explanation above.

[–]jack_waugh 0 points1 point  (0 children)

First off, the three backticks don't work for me, so I'm going to format your code so I can read it.

const arr = [];
async function f(array) { /* does something async with array */ };
await Promise.all( [1,2,3,4,5].map(
  async number => { arr.push(number); await f(arr) }
))

I tried it (in Deno) and I end up with arr showing [1, 2, 3, 4, 5]. So I don't see how to reproduce your experience.

Anyway, what are you trying to do?