如何使用Angular中的RXJ启用数据,直到收到数据,直到收到数据

发布于 2025-02-03 05:32:12 字数 640 浏览 4 评论 0原文

有一个API,已发送HTTP请求,并且很长一段时间没有响应,但是当用户刷新页面时,API会直接响应此行为,因此我想使用RXJS克服此问题。当API在40秒内未响应时,应自动重试HTTP请求&应该尝试3次。如何使用RXJ

在这里实现此行为是代码:

abc.service.ts

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

xyz.component.ts

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

帮助我解决这个问题

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. when the API does not respond within 40 seconds it should automatically retry the HTTP request & it should be attempted 3 times. how to achieve this behavior using RXJS

Here is the code:

ABC.service.ts

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

XYZ.component.ts

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

Help me with this Issue

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2025-02-10 05:32:12

请查看在此处发布的解决方案:

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

需要调整超时的时间和重试的数量。

Please have a look at the solution already posted here:

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

You only need to adjust the time for the timeout and the number of retries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文