How to await in a for loop or forEach

Posted on December 1, 2021

TIL I've learned how to use async/await and a for-loop.

Suppose you have something like:

const arr = [1, 2, 3]
const res = []

arr.forEach(async v => {
  const r = await getSomething()
  res.push(r)
})

console.log(res)

The above code doesn't work as expected. ForEach will not wait the resolution of all its await statements.

Here the solution:

const arr = [1, 2, 3]
const res = []

for(const v of arr)
  const r = await getSomething()
  res.push(r)
})

console.log(res)

The for of make the trick!