I'm trying to throw an error inside a rxjs Observable
new Observable(subscriber => { Observable.throw("error1"); return Observable.throw("error2"); }) .subscribe( () => {}, err => console.error(err) );
error 1 is not caught.
error2 gives a compilation error:
Argument of type '(this: Observable<{}>, subscriber: Subscriber<{}>) => ErrorObservable<string>' is not assignable to parameter of type '(this: Observable<{}>, subscriber: Subscriber<{}>) => TeardownLogic'. Type 'ErrorObservable<string>' is not assignable to type 'TeardownLogic'
what is the proper way to throw an error inside an observable?
Use Subscriber.error
:
new Rx.Observable(subscriber => { subscriber.error("error1"); subscriber.error("error2"); // not called because a stream will be finalized after any error }) .subscribe( () => {}, err => console.error(err) );
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
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