Skip to content Skip to sidebar Skip to footer

How Do I Wrap Axios.get Multiple Response Results Into One Array?

Hi I am trying to combine the results of my axios get request into one array. I am grabbing data from Mongoose and MongoDB database which returns me an array of relevant informatio

Solution 1:

You can make use of Promise.all which will return the response from all the requests once all of them are successful

const PromiseArr = [];
for (let i = 0; i < info.length; i++){

var url = "https://whattomine.com/coins.json?" + algo + "=true" + "&factor%5B" + algo + "_hr%5D=" + info[i]
  PromiseArr.push(
   axios.get(url).then(result => new Promise(resolve => resolve(result.data.coins.Monero.btc_revenue)))
  );
}

Promise.all(PromiseArr).then(res => {
    console.log(res)
});

Solution 2:

If you can use async/await, you can simply create an array and add the results one by one like so:

const result = [];

for (let i = 0; i < info.length; i++) {
  const url = "https://whattomine.com/coins.json?" + algo + "=true" + "&factor%5B" + algo + "_hr%5D=" + info[i]
  const response = await axios.get(url);

  result.push(response.data.coins.Monero.btc_revenue);
}

console.log(response);

Post a Comment for "How Do I Wrap Axios.get Multiple Response Results Into One Array?"