How to resumeOnError (or similar) in RxJS5

老子叫甜甜 提交于 2020-01-02 10:32:50

问题


I would like to use the onErrorResumeNext feature of RxJS, i.e. to continue to receive events even if an error is received (instead of terminating).

But I can see in the following doc that there is no correspondance in RxJS5: https://github.com/ReactiveX/RxJS/blob/master/MIGRATION.md.

Is there a workaround to use such feature? Thanks!


回答1:


I've been looking for that operator too! I came up with a solution for my needs that I hope will help yours. It's not the best solution most likely, but I hope it can help until a better solution can be found by some others on here, or until this operator is added back in 5.0 (hopefully!) :)

var Observable = Rx.Observable;

var source1 = Observable.create(function(observer){
    observer.error();
});
var source2 = Observable.create(function(observer){
    observer.next('Continuing on');
    observer.complete();
});


var stream = source1.catch(function(data){
  return source2;
});



stream.subscribe(function(data){
  console.log(data);
});

JS Bin Example: https://jsbin.com/qadozoveta/edit?html,js,console




回答2:


You can use the following in rxjs5 still.

 Observable.onErrorResumeNext(observable1$, observable2$, observable3$...)
           .subscribe(() => { ... })

onErrorResumeNext source code



来源:https://stackoverflow.com/questions/35772998/how-to-resumeonerror-or-similar-in-rxjs5

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