Difference between catch and onErrorResumeNext

本小妞迷上赌 提交于 2019-12-23 08:33:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/26634004/difference-between-catch-and-onerrorresumenext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!