Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between catch and onErrorResumeNext

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?

like image 856
Sergi Mansilla Avatar asked Oct 29 '14 15:10

Sergi Mansilla


1 Answers

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.

like image 94
Dave Sexton Avatar answered Nov 15 '22 00:11

Dave Sexton