const Observable = rxjs.Observable; const observable = new Observable(function(observer){ let index = 0 let timer = setInterval( function(){ observer.next(index++) if (index === 3){ // observer.error('发生错误') observer.complete() clearInterval(timer) } },1000) }) const observer = { next: function(value){ console.log(value) }, error:function(value){ console.log(value) } } observable.subscribe(observer)