RxJS timeout without error when no return value is requested
我正在进行不返回值的Web api调用,我只对它们的HTTP状态代码感兴趣。
但是我希望这些调用在指定的时间间隔后超时。
因为我错过了阅读文档的时间,所以我在可观察到的网络呼叫中使用了timeout-operator,如下所示:
1 2 3 | this.someProvider.requestStatus(someValue) .pipe(timeout(this.timeoutMilliseconds)) .subscribe(() => console.log("Success"),...) |
问题是,我在订户函数中收到了成功的Web调用,但是即使在超时时间段之后,调用仍然失败,因为可观察到的源未返回值-我认为。
当我没有获得HTTPResponse而只有正文-如果有的话,有没有办法用rxjs运算符来做到这一点?
如果源Observable在一段时间内未发出,则
但是如果源完成,则
尝试使用
1 2 3 4 5 6 7 8 9 | 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(.....) |
这将捕获错误并且不执行任何操作。
1 2 3 4 5 6 7 8 9 10 11 | 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); } }); |
正如您在评论中提到的。您可以检查错误实例以进行超时操作。但是您应该注意,如果发生超时,这将取消请求。