Home > Back-end >  Manipulating API in Vue methods
Manipulating API in Vue methods

Time:08-06

I have a method that is making 2 API calls simultaneously. How can I take the results of the API calls and manipulate them in another method within the same Vue component.

 buildChart(){
    *method where calls would be manipulated"

}
     async updateChart() {
        this.isLoading = true;
        const apiCall1 = await get().then((result) => {
          console.log(result);
          this.isLoading = false;
        });
        const apiCall2= await get().then((result) => {
          console.log(result);
          this.isLoading = false;
        });
      }

CodePudding user response:

You can use Promise.all to execute the two requests in parallel, the result will be an array of two items of each async request.

async buildChart(){
  // here result will be an array with two items of the result of each async get method.
  const [firstRequest, secondRequest] = await updateChart()
  this.isLoading = false;
},
updateChart() {
  this.isLoading = true;
  return Promise.all([get(), get()])
}

  • Related