K
K
kirillsnovikov2019-01-23 16:27:42
JavaScript
kirillsnovikov, 2019-01-23 16:27:42

How to make multiple requests at the same time in Vue+Axios?

Hello!
Tell me, is it possible to make several requests in parallel in one Vue component?
I assume that it is possible, but how good is this approach or is it still better to make each request in a separate component?
The code from this example did not help.
Thank you for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2019-01-23
@kirillsnovikov

Promise.all([
  axios.get('/user/12345'),
  axios.get('/user/12345/permissions')
]).then(([
  user,
  permissions
]) => {
  console.log(user, permissions);
  ///
});

A
Andrey Okhotnikov, 2019-01-23
@tsepen

Multiple simultaneous requests can be made using axios.all

function getUserAccount() {
  return axios.get('/user/12345');
}

function getUserPermissions() {
  return axios.get('/user/12345/permissions');
}

axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
    // Both requests are now complete
  }));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question