官术网_书友最值得收藏!

Subscribing using the log method

So far, every time we created an observable, we used the onValue() method to listen to events in the observable. In our examples, we usually just printed the value to the console,as this is common usage when testing observables and operators bacon.js has a special method through which you can print all the events to the console. All observables have the log() method. This method prints every event to the console and prints the <end> string when the event stream finishes. We can use it with EventStreams:

Bacon 
.fromArray([1,2,3,4,5])
.log();

This code gives you the following output to the console:

    1
2
3
4
5
<end>

As you can see, after all the events, it will print the <end> string to indicate the end of the EventStream. If we decide to use it with an infinite stream (a stream created with the interval() method), it will never print the <end> string (as you should expect). Refer to the following code:

Bacon 
.interval(100)
.log()

This will print the following output:

    {}
{}
{}
{}

It will keep on printing until you close the program.

We can also use the log() method with a Property, as you can see in the following example:

var stringProperty = Bacon 
.fromArray(['a','b','c','d'])
.scan('=> ',(acc,b)=> acc+b);

stringProperty.log();

This will print the following output:

    =>
=> a
=> ab
=> abc
=> abcd
<end>

As you can see, this prints the <end> string for a Property as well.

The log() method is especially useful for debug purposes and to test and see how an operator works. It is the fastest way to see an operator in action.
主站蜘蛛池模板: 元朗区| 喜德县| 石景山区| 临沭县| 施秉县| 鄂托克前旗| 龙井市| 新巴尔虎右旗| 乌鲁木齐县| 壤塘县| 宁安市| 丹棱县| 桃园县| 大名县| 泰安市| 叶城县| 香河县| 全南县| 浠水县| 垫江县| 崇文区| 石楼县| 繁昌县| 澄江县| 萨嘎县| 城步| 绥中县| 新野县| 巴东县| 莱芜市| 黑龙江省| 扬中市| 乌兰察布市| 德江县| 江达县| 牟定县| 江口县| 光山县| 清徐县| 凤山县| 瑞昌市|