Categories
JavaScript Answers

How to call an asynchronous function within map with JavaScript?

Spread the love

To call an asynchronous function within map with JavaScript, we use the for-of loop.

For instance, we write

const promises = teachers.map(async (m) => {
  return await request.get("...");
});

for (let val of promises) {
  //....
}

to call teachers.map with a callback to return an array of promises.

Then we use the for-of loop to loop through each promise in the promises array and run them.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *