1
votes

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?

3
consider using the takeUntil operatorC_Ogoo
@KarthigeyanVellasamy yes, i do not get any value, because the api does not send values, though the success-"callback" is still executed, because the call was successfull... But after that the timeout-operator still throws an error, because no value was emitted.Velulian
So, if I understand correctly you don't want to see the error, right? @VelulianHerman Lule
@Herman Lule I don't want to it to be emitted when the call succeeds even though no value was omitted.Velulian
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.martin

3 Answers

1
votes

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.

1
votes

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.

1
votes
 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.