JavaScript Async forEach

Published on August 14th, 2019

javascript icon

I have recently worked on async data fetching where I needed to use forEach method to format every item in array. And it didn’t work. For good 5-6 hours. I’ve tried everything. Only to find that JavaScript forEach method doesn’t play nicely with async functions.

Then I’ve found this medium article, copied snippet of code and it magically started working!

And here I am, happy man sharing this solution for future self and all other people with same issue.

async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array)
  }
}

Enjoy! It just works™️.