-1

Observable only can be subscribed by one observer . Subject allows values to be multicasted to many Observers but in this example why foo was subscribed twice? Thank you!

enter image description here

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
buctwbzs
  • 41
  • 5
  • Of course one Observable can be subscribed multiple times. This the most basic principle of Rx. See at least the basics at http://reactivex.io/documentation/observable.html – martin Dec 20 '16 at 13:33
  • A "multicasted Observable" passes notifications through a Subject which may have many subscribers, whereas a plain "unicast Observable" only sends notifications to a single Observer. i read these in DOC – buctwbzs Dec 20 '16 at 14:47
  • And this is exactly what is happening in your example. You just have two observers subscribing to the same observable. Each of them receive the notification they are supposed to receive - check my answer. – user3743222 Dec 20 '16 at 14:50
  • add `.share()` and subscribe after the share, then it should only execute once for both subscribers – olsn Dec 20 '16 at 17:44

1 Answers1

0

Well foo is subscribed twice because you subscribe to it twice. It is a typical confusion for people starting with Rxjs. Subscribing any number of times to certain type of observables (called cold observables), will lead you to repeating the exact same process, which is what is happening here. To get a better understanding of the matter, you can refer to the question which has been dealt here : Hot and Cold observables : are there 'hot' and 'cold' operators?

Community
  • 1
  • 1
user3743222
  • 18,345
  • 5
  • 69
  • 75