Make Angular4 wait for callback to API without timeout

Asked

Viewed 614 times

0

People see if someone can help me.

I need to make a call on the API, but it’s a lot of data, and I want Angular to wait for that termination, At that moment I put a file but it does not wait and as the API is treating the data on the Nodejs side Angular performs a second call in the API by running over and creating two calls on the execution line. Code of my service:

buscarConsumo(dadosConsumo): Promise<any> {
return this.http.post(`${API_AZURE}/buscarconsumoperiodo`, dadosConsumo)
  .toPromise()
  .then((data) => data)
  .catch((e) => console.log('Erro na Service', e));
}

My API takes 3 to 4 minutes to finish, I will treat this in bank in the future, more at the precise moment bring the data to the client side, tried to put timeout more unsuccessfully.

  • If you want an asynchronous call to occur only after the other one is over, then just put the second one inside the then of the first

  • Blza Guilherme, so this second call seems to me something automatic from the angular, I call the API and it keeps running, after a time the angular got no answer he sends again, I want a way not to make this second call or increase my timeout

  • Tries to return to Promise or observable and do then logic in your component.

  • Blza @Eduardovargas? I did it and it worked

1 answer

0

With the advice of Eduardo worked, putting then in the component he waits for the end of the request on the server until the end:

buscarConsumoTotalResourceGroup(f) {

this.loading = true;
this.consumoService.buscarConsumoTotalResourceGroup(this.consumo)
.toPromise()
.then((consumoResourceG: any) => {
  this.consumoResourceG = consumoResourceG;
  this.loading = false;
  console.log('Retorno de Consumo Total por ResourceGroup: ', consumoResourceG);
});

}

Browser other questions tagged

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