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. Type 类型

    修改 type 类型 UPDATE  wd_order2 SET  info = array_append(info, row(2,100001, now() )::info )  WHERE id_ ...

  2. 数据结构练习 00-自测5. Shuffling Machine (20)

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  3. hadoop 常用配置项

    core-site.xml  name value  Description   fs.default.name hdfs://hadoopmaster:9000 定义HadoopMaster的URI ...

  4. hdu3949 XOR xor高斯消元

    XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 测试一下MarkDown

    欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接 ...

  6. 使用bacula实现Linux的远程备份和还原

    Bacula,被誉为开源软件中最好的备份还原软件,它提供了企业级的客户机/服务器的备份解决方案,能够通过网络来管理文件的备份,恢复和核实工作.Bacula,既有windows版本的,也有Linux,U ...

  7. bzoj1212

    trie树最基本的应用了不难得到f[i]=f[j] if (s[j+1~i]∈dictionary);可以用trie树匹配 ..] of boolean; son:..,..] of longint; ...

  8. WordPress 开放重定向漏洞

    漏洞名称: WordPress 开放重定向漏洞 CNNVD编号: CNNVD-201309-167 发布时间: 2013-09-13 更新时间: 2013-09-13 危害等级: 高危   漏洞类型: ...

  9. WordPress HMS Testimonials 多个跨站脚本漏洞和跨站请求伪造漏洞

    漏洞名称: WordPress HMS Testimonials 多个跨站脚本漏洞和跨站请求伪造漏洞 CNNVD编号: CNNVD-201308-199 发布时间: 2013-08-22 更新时间: ...

  10. BZOJ1599: [Usaco2008 Oct]笨重的石子

    1599: [Usaco2008 Oct]笨重的石子 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 758  Solved: 513[Submit][ ...