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');
}
);
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.
Example 1: Throw error on subscriptionconst source = throwError('This is an error! '); //output: 'Error: This is an error!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With