Reputation: 353
I am making web api calls which don't return values and only their HTTP status code is of interest to me. But i want those calls to timeout after a specified time span.
Because i missed reading the doc, i used the timeout-operator on the web-call-observable like so:
this.someProvider.requestStatus(someValue)
.pipe(timeout(this.timeoutMilliseconds))
.subscribe(() => console.log("Success"),...)
The thing is, that i receive the successfull webcall within the subscriber function, but even after the timeout time span the call still fails, because the source observable did not return a value - i think.
Is there a way to do this with the rxjs operators when i don't get the HTTPResponse back and only the body - if any?
Upvotes: 2
Views: 5841
Reputation: 96979
timeout()
will throw an error if the source Observable doesn't emit for a period of time.
But if the source completes then timeout()
will stop. So it looks like your source Observable this.someProvider.requestStatus()
doesn't complete.
Upvotes: 2
Reputation: 10790
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);
}
});
As you mention in the comments. You can check for error instance for timing-out operation. But you should notice that this will cancel the request if timeout occurs.
Upvotes: 1
Reputation: 71
Try using the catchError
operator like this:
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(.....)
This will catch the error and do nothing.
Upvotes: 1