[RxJS] Observables can throw errors

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(42);
    observer.next(100);
    observer.next(200);
    setTimeout(function () {
      observer.next(300);
    }, 1000);
  } catch (err) {
    observer.error(err);
  }
});

bar.subscribe(function nextValueHandler(x) {
  console.log(x);
}, function errorHandler(err) {
  console.log('Something went wrong: ' + err);
});

 

posted @ 2016-04-10 00:30  Zhentiw  阅读(352)  评论(0)    收藏  举报