Context:与Angular 9一起工作。我正在尝试等待后端以接收请求。同时,没有一个进度栏正在加载。
issue:当尚未可用后端时,订阅会立即失败并输入该方法的(错误)回调函数,并带有类似的消息。
我确实进行了一些研究,我发现的示例修改了存在的服务类,以重试x次请求,但是我不能这样做,因为TS服务是自动生成的。
我的第一个想法是使用()循环和标志,因此每次执行(错误),标志将是错误的,然后重试订阅。但这将导致内存泄漏。
.subscribe()
只要您的服务功能返回
Http failure response for http://localhost:5555/api/info: 504 Gateway Timeout
。
(请参阅Https://rxjs.dev/api/operators/retryconfig
):
httpClient.get
使用
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);
}
: