[RxJS] Error handling operator: catch

时间:2021-09-30 21:56:34

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. &lbrack;RxJS&rsqb; Error Handling in RxJS

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

  2. &lbrack;RxJS 6&rsqb; 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. &lbrack;RxJS 6&rsqb; 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. &lbrack;Angular&rsqb; Observable&period;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&ndash&semi;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. 初识GO语言——安装Go语言

    本文包括:1)安装Go语言.2)运行第一个Go语言.3)增加vim中对Go语言的高亮支持. 1.安装Go语言 本文采用源码安装Go语言,Go语言的源代码在百度网盘 http://pan.baidu.c ...

  2. 让Linux下的打印机hp1020、hp p1008自动加载固件

    前言: 前段时间,处理公司打印机服务器Linux化工作.遇到问题如下:hp1020.hp1008断电后不能继续打印.而其他打印机在连接Linux打印机的情况下,断电后也能正常打印. 鉴于此情况,我搜寻 ...

  3. 05顺序队列&lowbar;Queue--&lpar;栈与队列&rpar;

    #include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...

  4. JavaScript中的execCommand&lpar;&rpar;命令详解及实例展示

    execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其 ...

  5. 你的外接键盘的小键盘在Num Lock键亮着的,但是数字按了不能用,解决办法在这里

    1.可能是Num Lock键卡住了导致的,你多按几次numlock键试试. 如果上面的不行,你就再试试下面的这个: 2.系统下开启了启用鼠标键导致的,解决的方法如下: (1).打开"控制面板 ...

  6. 【转】JavaScript 之arguments、caller 和 callee 介绍

    1.前言 arguments, caller ,   callee 是什么? 在JavaScript 中有什么样的作用?本篇会对于此做一些基本介绍. 本文转载自:http://blog.csdn.ne ...

  7. C&num; Lambda表达式运用

    原文作者: C# Lambda表达式 原文作者2: Lambda表达式详解 Lambda表达式 "Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lamb ...

  8. ABP&plus;AdminLTE&plus;Bootstrap Table权限管理系统第五节--WBEAPI及SwaggerUI

    一,Web API ABP的动态WebApi实现了直接对服务层的调用(其实病没有跨过ApiController,只是将ApiController公共化,对于这一点的处理类似于MVC,对服务端的 调用没 ...

  9. 《CSS世界》读书笔记(十六)

    <!-- <CSS世界>张鑫旭著 --> line-height与“垂直居中” line-height 可以让单行或多行元素近似垂直居中,原因在于 CSS 中“行距的上下等分机 ...

  10. C语言入门(1)

    开始学习C语言 第一个C语言程序 #include<stdio.h> int main() { printf("Hello World!"); } C程序结构 1. 头 ...