Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference with observer.throw and observer.error in Observable?

What is difference with observer.throw(err) and observer.error(err) ?

I'm using RxJS version "5.0.0-beta.12"

var innerObservable = new Observable(observer => {
    console.log('Inner observable call failed');
    observer.error(new Error('Call failed!'));
})


var outerObservable = new Observable(observer => {
    innerObservable.subscribe(
        data => {
            observer.next(data);
            observer.onCompleted();
        },
        err => {
            //observer.throw(err); // `console.error` doesn't get called
            observer.error(err);  //// `console.error` it's called
        }
    )
});

outerObservable.subscribe(
    next => {
        console.log('ok!');
    }
    , err => {
        console.error('error');
    }
    , () => {
        console.log('done');
    }
);
like image 922
Mohsen Sarkar Avatar asked Jan 31 '17 06:01

Mohsen Sarkar


People also ask

What is difference between observer and Observable?

Observer : Any object that wishes to be notified when the state of another object changes. Observable : Any object whose state may be of interest, and in whom another object may register an interest.

How do you throw an error in subscribe?

Example 1: Throw error on subscriptionconst source = throwError('This is an error! '); //output: 'Error: This is an error!

What is difference between observer and Observable in angular?

Observable are just that — things you wish to observe and take action on. Angular uses the Observer pattern which simply means — Observable objects are registered, and other objects observe (in Angular using the subscribe method) them and take action when the observable object is acted on in some way.


1 Answers

In fact your observer is an instance of Subscriber class. It has error() method but there's no throw() method.
There's Observable.throw() but that's something different.

  • observer.throw(err) doesn't exist.

  • Observable.throw(err) is an observable that emits just a single error notification.

  • observer.error() is typically used inside Observable.create() callback to send an error notification (just like .next() or .complete) to the newly subscribed observer.

like image 198
martin Avatar answered Sep 27 '22 17:09

martin