In RxJS, there seems to be very little difference between an Observable instance's catch
method and onErrorResumeNext
method, besides the fact that onErrorResumeNext
concatenates the original Observable with the Observable parameters whether an error happens or not.
If that's the case, isn't the naming a bit confusing? Because in case there is an error , onErrorResumeNext
works exactly the same way than catch
does:
var testObservable = Rx.Observable.return(1).concat(Rx.Observable.throw("Error"))
// Both onError and onCatch will emit the same result: 1, 2
var onError = testObservable.onErrorResumeNext(Rx.Observable.return(2));
var onCatch = testObservable.catch(Rx.Observable.return(2));
Is there a strong reason for not always using catch
?
They are distinct.
As you've noted, both operators handle failure similarly; however, they differ in their handling of completion.
OnErrorResumeNext
is simply a specialization with the following semantics:
"My query concatenates two observables. If the first observable fails, then resume with the next observable anyway."
Catch
is far more general:
"My query avoids failure. If the observable fails, then continue with another observable."
If you're avoiding failure on the boundary of concatenation, then use OnErrorResumeNext
; otherwise, to avoid failures in general use Catch
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With