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. 桂电在线-转变成bootstrap版2(记录学习bootstrap)

    下载bootstrap框架https://github.com/twbs/bootstrap 或者 http://getbootstrap.com/ 拷贝模板 修改基本模板 语言zh-cn,标题,描述 ...

  2. JSON字符串序列化与反序列化浅试

    一.添加引用(using Newtonsoft.Json.Linq;) 二. 1.生成json字符串源码 List<string> list = new List<string> ...

  3. Uva_11762 Race to 1

    题目链接 题意: 给一个数n, 每次从小于等于n的素数里选一个P, 如果能被n整除, 那么就n就变成n / P. 问: n 变成1的期望. 思路: 设小于等于n的素数有p 个, 其中是n的约数的有g个 ...

  4. 10300 - Ecological Premium

    Problem A Ecological Premium Input: standard input Output: standard output Time Limit: 1 second Memo ...

  5. ubuntu下为opera26.0安装flash

    因为 Adobe Flash 不再支持 linux Google 便开发了PepperFlashPlayer来替代原来的 Adobe Flash 下面介绍 PepperFlashPlayer 在安装方 ...

  6. c# 测试

    string input = "//a/@href "; int index = input.IndexOf("/@"); String attr = inpu ...

  7. ios7新特性1-UI变化、UIKit动态行为支持与Text Kit新接口

    iOS 7.0新特性1 iOS 7的UI经过了重新设计.另外,iOS7中引入了新的动画系统,便于创建2D和2.5D的游戏.多任务支持提升,点对点通讯以及其他重要的特征使iOS7相对于以往的SDK来说发 ...

  8. Codeforces Round #154 (Div. 2) : B

    一个很简单的题: 方法一: 二分. 代码: #include<cstdio> #include<algorithm> #define maxn 100005 using nam ...

  9. net.sf.json在处理json对象转换为普通java实体对象时的问题和解决方案

    我使用的net.sf.json是json-lib-2.4-jdk15.jar,把json对象转换为普通java实体对象时候有个问题,josn对象转换为java对象之后,json串里面的那几个小数点的值 ...

  10. 14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB

    14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB 14.6.11.1 Configuring Persisten ...