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

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.
主站蜘蛛池模板: 康定县| 威宁| 休宁县| 固安县| 凤庆县| 芷江| 南昌市| 唐海县| 松原市| 罗江县| 松潘县| 甘谷县| 镇原县| 扶绥县| 遂宁市| 惠来县| 大石桥市| 弋阳县| 保康县| 金门县| 伊宁县| 延川县| 海丰县| 吴旗县| 红桥区| 抚顺县| 宁蒗| 崇礼县| 怀远县| 安阳县| 开平市| 黄龙县| 孟津县| 丰宁| 隆子县| 航空| 镶黄旗| 大关县| 太谷县| 松滋市| 页游|