[RxJS] Error handling operator: catch
Most of the common RxJS operators are about transformation, combination or filtering, but this lesson is about a new category, error handling operators, and its most important operator: catch().
Basic catch( err => Observable):
var foo = Rx.Observable.interval(500)
.zip(Rx.Observable.of('a','b','c','d',2), (x,y)=>y); var bar = foo.map(x => x.toUpperCase()); /*
--a--b--c--d--2| (foo)
map(toUpperCase)
--A--B--C--D--# (bar)
catch(# => ###|)
--A--B--C--D--###|
*/ var result = bar.catch(error => Rx.Observable.of('###')); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
);
Retry with catch( (error, outputObs) => Observable):
var foo = Rx.Observable.interval(500)
.map( () => Math.random()); var bar = foo.map(x => {
if(x < 0.5){
return x;
}else{
throw "Error, too large, try again"
}
}); var result = bar
.catch(
(error, outputObs) => {
return outputObs;
}
); result.subscribe(
function (x) { console.log('next ' + x); },
function (err) { console.log('error ' + err); },
function () { console.log('done'); },
);
Code check whether the x is large than 0.5, if is then throw error, if not, then continue.
bar$ catch the error and use 'outputObs' to repeat the action, so evenytime, it hits the error, it will restart.
[RxJS] Error handling operator: catch的更多相关文章
- [RxJS] Error Handling in RxJS
Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thro ...
- [RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator
Sometime we want to set a default or fallback value when network request failed. http$ .pipe( map(re ...
- [RxJS 6] The Retry RxJs Error Handling Strategy
When we want to handle error observable in RxJS v6+, we can use 'retryWhen' and 'delayWhen': const c ...
- [Angular] Observable.catch error handling in Angular
import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...
- Erlang error handling
Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...
- Error Handling
Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...
- Error Handling and Exception
The default error handling in PHP is very simple.An error message with filename, line number and a m ...
- Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...
- beam 的异常处理 Error Handling Elements in Apache Beam Pipelines
Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...
随机推荐
- JavaScript学习心得(十)
Ajax Ajax是浏览器中使用JavaScript进行服务器后台请求,读取附加信息或者导致服务器响应的过程. Ajax广泛用于从服务器读取数据,并用所得到的数据更新页面,以及向服务器发送数据 Aja ...
- JavaScript学习心得(八)
Cookie是Netscape发明的技术,是动态网站必不可少的部分,用于浏览器请求Web页面的超文本传输协议是一种无状态的协议. 两种方法维护状态:使用会话(session)(使用服务器技术实现,数据 ...
- Activity singleTop启动模式
栈顶单例模式 和standard模式一样, 只有Activity已经存在并且位于栈顶时, 不会重新创建. 其他时候都会创建新的Activity,然后放在栈顶
- 正确安装 django-socketio
直接使用 pip 安装,连 example project 都运行不了... 要正常使用,关键是要使用正确版本的依赖包 Django (1.5.5) django-socketio (0.3.2) g ...
- PyQt5创建第一个窗体(正规套路)
一.Pyqt5 创建第一个窗体 很多人写窗体程序都是直接敲代码,不使用设计器,我个人不是很赞成这种做法.使用设计器的好处是直观.维护方便,尤其开发复杂窗体的效率高. 但是每次修改ui文件后,需要重新生 ...
- 2016022613 - redis连接命令集合
redis连接命令 1.ping 用途:检查服务器是否正在运行 返回数据pong,表示服务器在运行. 2.quit 用途:关掉当前服务器连接 3.auth password 用途:服务器验证密码 没有 ...
- 使用ListItem给DropDownList填充数据
global::日积月累啥的啊.DBhelper db = new 日积月累啥的啊.DBhelper(); ListItem[] item=]; DataTable dt=db.GetDataTabl ...
- jQuery 表格插件25
jQuery 表格插件可以让你创建各种各样的表格布局,表格布局是报纸和杂志中最常见的布局,现在的网站中也很常见,在这篇文章中,我向大家推荐25个jQuery 的表格插件,你可以任意控制表格的行和列,用 ...
- OpenJDK与HashMap
OpenJDK的非堆JDK增强提议(JDK Enhancement-Proposal,JEP)试图标准化一项基础设施,它从Java6开始,只能在HotSpot和OpenJDK内部使用.这种设施能够像管 ...
- Web-Scale-IT 到底是啥?
Gartner 对 2015 年 10 大 IT 趋势的预测中有一个词条为:Web Scale IT.我们跟随 Matthias Ankli 来了解一下究竟什么是 Web Scale IT.本文译自 ...