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

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.
主站蜘蛛池模板: 屏东县| 商都县| 凤阳县| 景德镇市| 乌海市| 绵阳市| 桐城市| 太湖县| 陕西省| 太和县| 龙陵县| 怀柔区| 朝阳县| 河南省| 大荔县| 邢台市| 汪清县| 揭阳市| 陕西省| 色达县| 马关县| 色达县| 太保市| 三原县| 普宁市| 内乡县| 濮阳市| 常德市| 临沂市| 齐齐哈尔市| 巴塘县| 泰和县| 延吉市| 马龙县| 灌阳县| 荔浦县| 德化县| 惠东县| 郎溪县| 离岛区| 宁国市|