【问题标题】:How to cancel API calls that take more than 10 ms to respond in interceptor如何取消在拦截器中响应时间超过 10 毫秒的 API 调用
【发布时间】:2020-12-27 20:35:54
【问题描述】:

我已经尝试使用takeUntil rxjs 来实现它,

当我多次点击Call API按钮时,它不会在一定时间后取消所有API调用。

Stackblitz 链接 - Code Example

问题 1 - 根据上面的 Stackblitz 链接,我可以看到它会取消检查网络选项卡中的 API 调用,但不会从拦截器文件中的 setTimeout 为每个请求取消。

问题 2 - 处理此要求的任何其他最佳解决方案?

【问题讨论】:

    标签: angular rxjs angular7


    【解决方案1】:

    如下图,可以使用Timeout操作符(如果Observable在指定的时间段内没有发出值会抛出错误)。

    import { timeout } from 'rxjs/operators';
    
    return next
      .handle(request)
      .pipe(
        timeout(1000 * 10),
        catchError(err => {
          ....
        }),
      )
    

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      • 2020-10-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      相关资源
      最近更新 更多