Reacting to changes
As discussed earlier, an observable is an object where you can listen to events. The act of listening to events in this object is called subscription. Here, we will see the different ways in which we can subscribe to an observable and also how we can stop listening to events in this observable (unsubscribe).
Subscribing
We call subscribing to an observable an act of adding a function to be called when an event happens. Using bacon.js, we can be notified when a value is emitted (the onValue()
, log()
, and assign()
methods), when an error has occurred (the onError()
method), or when our observable is closed (at the end).
Subscribing using the onValue() method
The most common way of subscribing to an observable is using the onValue()
method. This method has the following signature:
observable.onValue(functionToBeCalledWhenAnEventOccurs);
So let's subscribe to eventStream
to log every event on this stream, as follows:
Bacon .fromArray([1,2,3,4,5]) .onValue((number)=>...