How to make several requests in parallel in Xios?

Asked

Viewed 2,560 times

3

I have two urls to request: https://api.tuuris/cities and https://api.tuuris/expenses

I would like to request 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
  })

1 answer

7

Just provide an array for the method axios.all.

Then use the method axios.spread, that 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
 }))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.