- Mastering Reactive JavaScript
- Erich de Souza Oliveira
- 126字
- 2021-07-09 20:33:09
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.
推薦閱讀
- 大學計算機應用基礎實踐教程(Windows 7+Office 2013)
- 精解Windows 8
- 精解Windows8
- 嵌入式Linux驅動程序和系統開發實例精講
- 網絡操作系統管理與應用(第三版)
- Linux內核觀測技術BPF
- 深入淺出Node.js
- Linux設備驅動開發
- 計算機應用基礎(Windows 7+Office 2016)
- HTML5 Enterprise Application Development
- 鴻蒙操作系統設計原理與架構
- μC/OS-III內核實現與應用開發實戰指南:基于STM32
- VMware vSphere 5.1 Cookbook
- Azure Resource Manager Templates Quick Start Guide
- 辦公自動化教程(Windows7+Office2010)