When I create an observable from scratch, and have the observer error, then complete, the done part of the subscription never is invoked.
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:
errored with Error: no!
I'd expect it to be:
errored with Error: no!
completed
If I change the code to invoke onNext instead of onError, the observable properly completes:
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:
succeeded with Hi!
completed
Why does it not complete when an error has occured?
While I was having the same question, I bumped into this github issue.
Apparently
finally
method ofObservable
object needs to be used in this case.Quoting from Aleksandr-Leotech from that thread:
That's because an error means completion, so the callback associated to
onCompleted
never gets called. You can review here Rxjs contract for observables (http://reactivex.io/documentation/contract.html) :For error management, you can have a look at : https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/errors.md