在Angular 9中失败时,如何重试订阅?

问题描述 投票:0回答:1

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);
  }
angular rxjs observable subscription angular9
1个回答
11
投票
retry


请以这种方式尝试。使用RXJS/Operators的RXJS RETRY()。
https://angular.io/guide/rx-library#retry-failed-observable
    

这是我想要的最紧密的解决方案:

retryWhen
    
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.