O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)=>console.log(number)); 

This code gives you the following output to the console:

    1    2    3    4    5  

This function can be used for any type of observable (EventStream and Property). The only difference is that in Properties, if the initial value of the Property exists, then it triggers the onValue() function. Check out the following code:

var initialValue =0; Bacon     .fromArray([1,2,3,4,5])  .toProperty(initialValue) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required