Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS - observable doesn't complete when an error occurs

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?

like image 360
Oved D Avatar asked Nov 18 '15 15:11

Oved D


People also ask

Does Observable complete on error?

The Observable Contract and Error Handling Network requests can fail, for example. A stream can also complete, which means that: the stream has ended its lifecycle without any error. after completion, the stream will not emit any further values.

How do you handle errors in Observables?

Catch errors in the observable stream Another option to catch errors is to use the CatchError Operator. The CatchError Operators catches the error in the observable stream as and when the error happens. This allows us to retry the failed observable or use a replacement observable.

How do you finish Observable?

takeUntil(end$); When you want to end observable , you do end$. onNext("anything you want here"); . That is in the case the ending event is generated by you.

Is Observable lazy?

No, they aren't lazy, but they are asynchronous.


2 Answers

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) :

An Observable may make zero or more OnNext notifications, each representing a single emitted item, and it may then follow those emission notifications by either an OnCompleted or an OnError notification, but not both. Upon issuing an OnCompleted or OnError notification, it may not thereafter issue any further notifications.`

For error management, you can have a look at : https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/errors.md

like image 113
user3743222 Avatar answered Sep 21 '22 09:09

user3743222


Another and probably the simplest solution might be using the add() function.
The statement will be always executed regardless an error occured or not (like the finally statement in most programming languages).

observer.subscribe(     function(x) { console.log('succeeded with ' + x ) },     function(x) { console.log('errored with ' + x ) },     function() { console.log('completed') } ) .add(() => {     console.log("Will be executed on both success or error of the previous subscription") ); 
like image 43
Just Shadow Avatar answered Sep 20 '22 09:09

Just Shadow