最佳答案
当我从头开始创建一个可观察的,并且有观察者错误,然后完成时,订阅的已完成部分永远不会被调用。
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') }
)
输出结果是:
errored with Error: no!
我想应该是:
errored with Error: no!
completed
如果我将调用 onNext 的代码改为调用 onError,那么可观察代码将正确地完成:
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') }
)
我得到了预期的输出:
succeeded with Hi!
completed
当发生错误时,为什么它不完成?