RXJS 5 .subscribe() without arguments

纵然是瞬间 提交于 2019-12-10 01:59:22

问题


So a quick question. I've been using RxJS 5 for a few months now, and I've run into a bit of behavior that I don't really understand, as I haven't been able to look it up anywhere.

I'm in a situation where subscribing to an observable chain with simply .subscribe(); doesn't trigger the observable.

However, if I add an onNext callback (empty or not), the observable triggers, and the chain processes: .subscribe(() => {});

Can anyone explain why this behavior happens?

EDIT2 - Removed irrelevant example


回答1:


.subscribe() actually doesn't require any parameters, it will just create an emptyObserver if none are given, which should work just as well.

It is possible though, that there are issues related to this in some of the 5.0.0-beta versions - in case you are using one of those versions, you should update to a more stable release.

const obs$ = Rx.Observable.of("Foo")
  .do(() => console.log("triggered!"));
  
obs$.subscribe();
obs$.subscribe();
<script src="https://unpkg.com/rxjs@5.1.1/bundles/Rx.min.js"></script>



回答2:


Thanks to Damian Hercun for informing that this was a now-fixed bug in RxJS 5.4.2.

Info:

https://github.com/ReactiveX/rxjs/pull/1935

https://github.com/ReactiveX/rxjs/issues/1921



来源:https://stackoverflow.com/questions/42333187/rxjs-5-subscribe-without-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!