How to use async await on map function

You, JavaScriptES6
Back

The built-in map function in JS does not provide a _async_ and _await_ out of the box so here is how you should do it.

const functionName = results => {
  const promises = results.map(async result => asyncFunctionName(result));
  return Promise.all(promises);
};

Define the async function.

const asyncFunctionName = async ({});

Now you can call:

const data = await functionName(results);
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.