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

Observables' lazy evaluation

In bacon.js, an observable doesn't emit any event unless someone is subscribed to listen to it. We can easily check this behavior using doAction(). This operator lets us run an arbitrary function every time an event is emitted from an observable.

If you create an observable from an array and add a doAction() operator to log your events then, without a subscription, it will print nothing to the console. So if you run the following code, you will see your program doesn't give you any output:

Bacon 
.fromArray([1,2,3])
.doAction((value)=>
console.log('running doAction')
);

As you can see in this code, we have no subscriber for the EventStream. This is why no event is emitted. However, if we add a subscriber to this EventStream, we will see the program print running doAction for each value:

Bacon 
.fromArray([1,2,3])
.doAction((value)=>
console.log('running doAction')
)
.log();

The preceding code gives you the following output:

    running doAction
1
running doAction
2
running doAction
3
<end>
It is really important to keep this behavior in mind because ignoring this can lead to bugs that are really hard to identify.
主站蜘蛛池模板: 定安县| 科技| 靖边县| 互助| 巢湖市| 天镇县| 九江县| 保山市| 陈巴尔虎旗| 阿勒泰市| 佛学| 蓬莱市| 丰城市| 蒙城县| 垦利县| 长垣县| 桑植县| 简阳市| 普安县| 黔西县| 雷山县| 绿春县| 洪雅县| 嘉鱼县| 太和县| 石首市| 宁远县| 台安县| 香港| 青田县| 清丰县| 明水县| 长武县| 湘潭县| 梅河口市| 洛南县| 庐江县| 和静县| 万载县| 元阳县| 蕲春县|