Observable closed on error

ε祈祈猫儿з 提交于 2019-12-05 15:52:24

This is the expected behaviour. According to the documentation,

In an Observable Execution, zero to infinite Next notifications may be delivered. If either an Error or Complete notification is delivered, then nothing else can be delivered afterwards.

For the code above, it may be

this.appService
// error is caught, but the observable is completed anyway
.catch((err) => {
    console.error(err)
    return Observable.empty();
})
// re-subscribe to completed observable
.repeat()
.subscribe((comments) => console.log(comments));

But considering the expected behaviour, it is unpractical to use RxJS error handling to supply a continuous observable with non-critical error values. Instead, it may be changed to

setTimeout(() => {
    //You will see the error displayed by the component
    this.commentsObserver.next(new Error('Nice errroorr'));
}, 1000);

and

this.appService.commentsObservable.subscribe((comments) => {
    if (comments instanceof Error)
        console.error(comments);
    else
        console.log(comments);
});

The approach may vary depending on the actual case.

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