[Vuejs]-Multiple requests with nuxt-axios

0👍

you can wrap your multiple requests in Promise.all(). Put all your requests in an array like:

const reqs = [req1, req2, req3];

Then pass that array to a Promise.all() like this:

async apiCalls() {
  const reqs = [req1, req2, req3];
  const response = await Promise.all(reqs);
  // the response would be an array with each element being the response of the
     requset like [res1, res2, res3]
  ...
};

This way multiple requests act as a single request, therefore toggle the loading when all of them are resolved.

You can use this method only if your requests do not depend on each other (i.e. they can be called in parallel that is req2 does not use the req1 response and so on).

0👍

It is a feature to be added to @nuxtjs/axios plugin which is missing, I did id with axios.all method.

Leave a comment