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的更多相关文章

  1. [RxJS] Error Handling in RxJS

    Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thro ...

  2. [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 ...

  3. [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 ...

  4. [Angular] Observable.catch error handling in Angular

    import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...

  5. Erlang error handling

    Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...

  6. Error Handling

    Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...

  7. Error Handling and Exception

    The default error handling in PHP is very simple.An error message with filename, line number and a m ...

  8. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

  9. beam 的异常处理 Error Handling Elements in Apache Beam Pipelines

    Error Handling Elements in Apache Beam Pipelines Vallery LanceyFollow Mar 15 I have noticed a defici ...

随机推荐

  1. TMS IntraWeb 5.4.1.1 for XE6 (适配Intraweb14.0.32)

    文件夹内含有我自己已经编译好的bpl,仅供大家学习使用,请支持正版!!导入ParaInstalarXE6.groupproj后,逐个编译安装即可. 链接:http://pan.baidu.com/s/ ...

  2. Unity3D 解决c#脚本乱码

    怀着无比激动的心情下载了Unity3D,按照网上的教程试着制作我的第一个U3D"作品":camera绑定绘制GUI显示"Hello, World",很简单的例子 ...

  3. android ListView 多次调用 getView方法

    <ListView            android:layout_width="match_parent"            android:layout_heig ...

  4. (转载)Convolutional Neural Networks卷积神经网络

    Convolutional Neural Networks卷积神经网络 Contents 一:前导 Back Propagation反向传播算法 网络结构 学习算法 二:Convolutional N ...

  5. 从零开始学习MySQL2---MySQL的安装与配置(只有Windows)

    因为我电脑只装了Windows系统,故而,只整理了在Windows系统下的安装方式 截图比较麻烦,故而多引用百度经验. Windows平台下安装与配置MySQL 5.6 下载,网址:http://de ...

  6. Contest20140710 loop bellman-ford求负环&&0/1分数规划

    loop|loop.in|loop.out 题目描述: 给出一个有向带权图,权为边权,求一个简单回路,使其平均边权最小. 简单回路指不多次经过同一个点的回路. 输入格式: 第一行两个整数,表示图的点数 ...

  7. ibatas的一些实例及解释

    Student.xml : <?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPE sqlMap PUB ...

  8. JVM参数配置大全

    前阵子遇到几个面试题都是关于对Java内存控制的,因此从网上找到这篇文章,希望自己对Java的内存分配有重新的认识 /usr/local/jdk/bin/java -Dresin.home=/usr/ ...

  9. BZOJ1612: [Usaco2008 Jan]Cow Contest奶牛的比赛

    1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 645  Solved: 433 ...

  10. Android应用连接代理服务器状况监测解决

    最近项目里面有这样一个需求,由于项目涉密需要连接VPN通过网址映射去登录内部服务器,而且要通知客户vpn的连接状态.网上有许多类似的连接VPN的解决方案,我也尝试了很多种,下面先列出一种比较靠谱的方式 ...