Context: Working with Angular 9. I'm trying to wait the back-end to be available to receive requests. Meanwhile it's not, there is a progress bar loading.
Issue: when the back-end is not yet available, the subscription fails immediately and enters the (error) callback function of the method .subscribe()
, with a message like Http failure response for http://localhost:5555/api/info: 504 Gateway Timeout
I did research a bit, and the examples I've found modify the service class where httpClient.get
is present, to retry x times the request, but I can't do that as the ts service is auto generated.
My 1st idea was to use while() loop and a flag, so each time (error) was executed, the flag would be false and then retry the subscription. But it would lead to a memory leak.
checkIfBackendAvailable() {
var backendAvailable = false;
while(!backendAvailable){
let subscription = this.infoService.getInfo().subscribe(
(info) => {
if (info) {
backendAvailable = true;
this.progressBarValue = 100
// do somethings
}
}
,
(error) => {
clearTimeout(this.infoTimeOut);
this.showMessage("back-end not available");
this.stopLoadingBar();
//do somethings
}
);
}
this.infoTimeOut = setTimeout(() => {
if (!backendAvailable) {
subscription.unsubscribe()
this.showMessage("error");
this.stopLoadingBar();
}
}, 120000);
}