简体   繁体   English

如何在Angular中使用RXJS重试http请求直到收到数据

[英]How to retry a http request untill a data is recieved using RXJS in Angular

There is an API where the HTTP request has been sent and there is no response for a long time but when the user refreshes the page the API responds immeditally this behavior occurs often, so I want to use RXJS to overcome this issue.有一个API发送了HTTP请求,很长时间没有响应,但是当用户刷新页面时,API立即响应这种行为经常发生,所以我想使用RXJS来解决这个问题。 when the API does not respond within 40 seconds it should automatically retry the HTTP request & it should be attempted 3 times.当 API 在 40 秒内没有响应时,它应该自动重试 HTTP 请求并且应该尝试 3 次。 how to achieve this behavior using RXJS如何使用 RXJS 实现这种行为

Here is the code:这是代码:

ABC.service.ts ABC服务.ts

 getABC() {
    return this.http
      .get(.........)
      .pipe(
        map(responseData => {
          return responseData
        }),
        catchError(errorRes => {
          return this.handleError(errorRes);
        })
      );
  }

XYZ.component.ts XYZ.component.ts

  this._abcService.getABC().subscribe((res: any[]) => {
     console.log(res);
    }, error => {
      console.log(error);
    });

Help me with this Issue帮我解决这个问题

Please have a look at the solution already posted here:请查看此处已发布的解决方案:

https://stackoverflow.com/a/50678363/2976617 https://stackoverflow.com/a/50678363/2976617

You only need to adjust the time for the timeout and the number of retries.您只需要调整超时时间和重试次数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM