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

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.
主站蜘蛛池模板: 莆田市| 原平市| 陇西县| 文水县| 定安县| 临澧县| 临夏县| 射阳县| 东城区| 东兴市| 张家港市| 图片| 怀柔区| 南乐县| 漠河县| 扶风县| 兰西县| 昌江| 恩施市| 和田市| 广灵县| 来安县| 吴川市| 石林| 莫力| 乌兰县| 江城| 嘉禾县| 莱西市| 新平| 钟祥市| 大港区| 攀枝花市| 都江堰市| 桃源县| 新龙县| 宁安市| 南城县| 元氏县| 都兰县| 清河县|