[RxJS] Observables can throw errors

时间:2023-03-08 18:08:58

Whenever we are writing code, we need to remember that things may go wrong. If an error happens in a function, that error will be thrown. Errors can also happen in Observables, and in this lesson we will see what is the API for throwing and catching them.

var bar = Rx.Observable.create(function (observer) {
try {
console.log('Hello');
observer.next();
observer.next();
observer.next();
setTimeout(function () {
observer.next();
}, );
} catch (err) {
observer.error(err);
}
}); bar.subscribe(function nextValueHandler(x) {
console.log(x);
}, function errorHandler(err) {
console.log('Something went wrong: ' + err);
});