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

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.
主站蜘蛛池模板: 旌德县| 车致| 灵寿县| 宜君县| 靖远县| 镇沅| 庆城县| 疏附县| 巴塘县| 驻马店市| 文登市| 筠连县| 铅山县| 视频| 定日县| 亳州市| 拜城县| 佳木斯市| 南城县| 甘孜县| 营口市| 安义县| 临桂县| 礼泉县| 吴忠市| 徐水县| 临沭县| 休宁县| 光泽县| 广东省| 府谷县| 定日县| 台江县| 平阳县| 公主岭市| 潍坊市| 神池县| 宿州市| 文登市| 连云港市| 凌源市|