Using async/await function with forEach

Sometimes, you want to loop asynchronous processing. forEach is often used to loop.
However, forEach doesn’t support async/await.
The bellow code is not working.

const getDataAll = async () => {
  ids.forEach(async id => await getData(id));
};

Because, forEach ignore the value of callback, generated promise from async, proceed not to await.
Therefore, if you want to solve this error, please do as follows.

const getDataAll = async () => {
  await Promise.all(ids.map(async id => await getData(id)))
};

You can solve parallel processing with Promise.all.
That’s it.

関連記事