Home > Mobile >  How to loop many http requests with axios in node.js
How to loop many http requests with axios in node.js

Time:11-28

I have an array of users where each user has an IP address.

I have an API that I send an IP as a request and it returns a county code that belongs to this IP.

In order to get a country code to each user I need to send separate request to each user.

In my code I do async await but it takes about 10 seconds until I get all the responses, if I don't do the async await, I don’t get the country codes at all. My code:

async function getAllusers() {
  let allUsersData = await usersDao.getAllusers();

  for (let i = 0; i < allUsersData.length; i  ) {
    let data = { ip: allUsersData[i].ip };
    let body = new URLSearchParams(data);
    await axios
      .post("http://myAPI", body)
      .then((res) => {
        allUsersData[i].countryCode = res.data.countryCode;
      });
  }

  return allUsersData;
}

CodePudding user response:

You can use Promise.all to make all your requests once instead of making them one by one.

let requests = [];
for (let i = 0; i < allUsersData.length; i  ) {
    let data = { ip: allUsersData[i].ip };
    let body = new URLSearchParams(data);
    requests.push(axios.post("http://myAPI", body)); // axios.post returns a Promise
  }
try {
    const results = await Promise.all(requests);
    // results now contains each request result in the same order
    // Your logic here...
}
catch (e) {
    // Handles errors
}

CodePudding user response:

If you're just trying to get all the results faster, you can request them in parallel and know when they are all done with Promise.all():

async function getAllusers() {
  let allUsersData = await usersDao.getAllusers();

  await Promise.all(allUsersData.map((userData, index) => {
      let body = new URLSearchParams({ip: userData.ip});
      return axios.post("http://myAPI", body).then((res) => {
          allUsersData[index].countryCode = res.data.countryCode;
      });
  }));
  return allUsersData;
}

Note, I would not recommend doing it this way if the allUsersData array is large (like more than 20 long) because you'll be raining a lot of requests on the target server and it may either impeded its performance or you may get rate limited or even refused service. In that case, you'd need to send N requests at a time (like perhaps 5) using code like this pMap() here or mapConcurrent() here.

  • Related