没有请求 return 值时 RxJS 超时无错误

RxJS timeout without error when no return value is requested

我正在进行网络 api 调用,这些调用没有 return 值,我只对它们的 HTTP 状态代码感兴趣。 但我希望这些调用在指定的时间跨度后超时。

因为我错过了阅读文档,所以我在 web-call-observable 上使用了超时操作符,如下所示:

this.someProvider.requestStatus(someValue)
    .pipe(timeout(this.timeoutMilliseconds))
    .subscribe(() => console.log("Success"),...)

问题是,我在订阅者函数中收到了成功的网络调用,但即使在超时时间跨度之后调用仍然失败,因为源可观察性没有 return 值 - 我认为。

当我没有返回 HTTPResponse 并且只有正文 - 如果有的话,有没有办法用 rxjs 运算符来做到这一点?

 this.someProvider.requestStatus(someValue)
    .pipe(
        timeout(this.timeoutMilliseconds)).subscribe(
        () =>console.log("Success"),
        err=> {
      if (err instanceof  TimeoutError ) {
        console.log("timed out");            
      } else {
        console.log("sth else happened",err);
      }         
    });

正如您在评论中提到的那样。您可以检查超时操作的错误实例。但是你应该注意到,如果发生超时,这将取消请求。

尝试像这样使用 catchError 运算符:

requestCall(...).pipe(
catchError(err => {
// Here you can check if the error corresponds to the one you don't want emitted
// And if so do nothing, in this case just return an empty array like
if(err.code === something) {
return []
}
// Or return the error
})).subscribe(.....)

这将捕获错误并且不执行任何操作。

timeout() 如果源 Observable 在一段时间内没有发射,将抛出错误。

但如果源完成,则 timeout() 将停止。所以看起来你的源 Observable this.someProvider.requestStatus() 没有完成。