当前位置:网站首页>Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs

Rxjs - observable doesn't complete when an error occurs - rxjs - observable doesn't complete when an error occurs

2022-07-07 07:34:00 fyin1314

problem :

When I create an observable from scratch, and have the observer error, then complete, the done part of the subscription never is invoked. When I create one from scratch observable And there are observer errors , Then when finished , The completed part of the subscription will never be called .

var observer = Rx.Observable.create(function(observer){    observer.onError(new Error('no!'));    observer.onCompleted();})observer.subscribe(    function(x) { console.log('succeeded with ' + x ) },    function(x) { console.log('errored with ' + x ) },    function() { console.log('completed') })

The output is: The output is :

errored with Error: no!

I'd expect it to be: I hope it is :

errored with Error: no!completed

If I change the code to invoke onNext instead of onError, the observable properly completes: If I change the code to call onNext instead of onError, be observable Will be completed correctly :

var observer = Rx.Observable.create(function(observer){    observer.onNext('Hi!');    observer.onCompleted();})observer.subscribe(    function(x) { console.log('succeeded with ' + x ) },    function(x) { console.log('errored with ' + x ) },    function() { console.log('completed') })

I get the expected output: I get the expected output :

succeeded with Hi! completed

Why does it not complete when an error has occured? Why did it not complete when an error occurred ?


Solution :

Reference resources : https://stackoom.com/en/question/2Hkkh
原网站

版权声明
本文为[fyin1314]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130656066290.html