[SOLVED] What is the correct to way to loop request with axios and async await?


I have about 500 URLs that I want to loop and get data from, the URLs are stored inside a const name urls:

const urls = []

I want to do something like this:

for (let i = 1; i <= url.length; i++) {
  const response = await axios.get(url);

response list is another const that I have outside the for loop.

This is actually working, but eslint has a rule for "no-await-in-loop" which makes me think I’m not doing the more optimized thing here.

What would be the best practice in this case?


Matia’s answer with just a little edit, remember to return inside promises:

const promises = urls.map(url => return axios.get(url));
const response = await Promise.all(promises);

Otherwise promises will be a list of undefines.

Answered By – Alvin

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

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