How to make multiple requests in parallel in axios?

3

I have two urls to make the request: link and link

I would like to make requests for both in parallel, currently I have done separately as in the code below:

  axios
  .get('https://api.tuuris/cities')
  .then(response => {
    this.info = response.data
  })
  .catch(error => {
    console.log(error)
    this.errored = true
  })
    
asked by anonymous 01.08.2018 / 18:50

1 answer

4

Simply provide an array for the axios.all method.

Then use the axios.spread method, which converts an array to multiple arguments.

$npm install axios

  axios.all([
    axios.get('https://api.tuuris/cities'),
    axios.get('https://api.tuuris/expenses')
    ]).then(axios.spread((citiesRes, expensesRes) => {
      this.cities = citiesRes.data
      this.expenses = expensesRes.data
 }))
    
01.08.2018 / 18:50