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

Acting when an error occurs

In the previous sections, we saw how to encapsulate an error on bacon.js. Now, we will use the OnError() method to take an action whenever this happens. This method has the same signature as that of the onValue() method and works similarly, but only for bacon errors. Now with this method, we can change the code we used in the previous section to print an error when it occurs. This is shown as follows:

var myCustomEventStream = Bacon.fromBinder(function(push){ 
push('some value');
push('other value');
push(new Bacon.Error('NOW AN ERROR HAPPENED'));
push('Now the stream will finish');
push(new Bacon.End());
});
myCustomEventStream
.onError((value)=>
console.log(value)
);

It will print the following:

    NOW AN ERROR HAPPENED
As you might expect, the OnError() method also returns a function to unsubscribe.
主站蜘蛛池模板: 舟曲县| 禹州市| 钟祥市| 潜山县| 贵港市| 射洪县| 尉犁县| 宜兴市| 台湾省| 梧州市| 德庆县| 赣州市| 长乐市| 于田县| 泾源县| 兰考县| 东阳市| 和硕县| 库尔勒市| 巴彦县| 滨州市| 资阳市| 公主岭市| 乐都县| 新密市| 淳安县| 博野县| 高碑店市| 柏乡县| 金坛市| 古交市| 历史| 南宁市| 上饶市| 横峰县| 揭阳市| 九龙坡区| 文山县| 绥芬河市| 阳谷县| 眉山市|