B
B
bqio2018-09-04 08:42:39
JavaScript
bqio, 2018-09-04 08:42:39

How to correctly format multiple requests in axios?

There is an array c url

let url = ["https://www.google.ru", "https://toster.ru"]

There is a request
axios.get(url).then(function (response) {
    console.log(response);
})

I do it like this:
for (let i = 0; i < url.length; i++) {
axios.get(url[i]).then(function (response) {
    console.log(response);
 })
}

How to do it right? The whole point is that the size of the array with url is initially unknown.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladlen Hellsite, 2018-09-04
@bqio

const urls = ["https://www.google.ru", "https://toster.ru"];

Promise.all(urls.map(url => axios.get(url)))
  .then(responses => console.log('Complete', responses));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question