博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJS] Error Handling in RxJS
阅读量:7195 次
发布时间:2019-06-29

本文共 3003 字,大约阅读时间需要 10 分钟。

Get your code back on the happy path! This lesson covers a variety of ways to handle exceptions thrown by Observables in RxJS. Operators covered are: catch, onErrorResumeNext, retry and retryWhen.

 

We have the code which throw error when hit 3. This error is catched in error block, so it not go to complete block, but image that we might have some side-effect to handle in complete block instead of just simple log.

Observable.of(1,2,3,4)  .map(x => {    if(x === 3) {      throw 'I hate threes';    }    return x;  })  .subscribe(    x => console.log(x),    err => console.error("err: " + err),    () => console.info('done')  );/*12"err: I hate threes"*/

 

So we need to handle the error and let the code go to the complete block: -- by catch(): 

Observable.of(1,2,3,4)  .map(x => {    if(x === 3) {      throw 'I hate threes';    }    return x;  })  .catch( err => Observable.just('catch: ' + err))  .subscribe(    x => console.log(x),    err => console.error("err: " + err),    () => console.info('done')  );/*12"catch: I hate threes""done"*/

Now the code goes to the complete block and we handle the error by using catch instead of error block. 

 

If we catch the error and still want error block to handle it we can use throw() instead od just():

Observable.throw('catch: ' + err)

 

---------------------

And we use catch(), but we didn't do anything about the error, so if you don't need to handle the error, just throw it, you can use onErrorResumeNext() function.

Observable.of(1,2,3,4)  .map(x => {    if(x === 3) {      throw 'I hate threes';    }    return x;  })  .onErrorResumeNext(Observable.just('There is an error!'))  .subscribe(    x => console.log(x),    err => console.error("err: " + err),    () => console.info('done')  );/*12"There is an error!""done"*/

 

-----------------------------------

Retry(numberofTimes): it will retry number of time before it goes to error. 

 

var { Observable } = Rx;var bad = Observable.throw('go bad');var good = Observable.just('go ahead!');Observable.of(1,2,3,4)  .map(x => {    if(x === 3) {      throw 'I hate threes';    }    return x;  })  .retry(3)  .subscribe(    x => console.log(x),    err => console.error(err),    () => console.info('done')  );/*121212"I hate threes"*/

 

 ----------------------------

retryWhen(observe): Retry after delay:

Observable.of(1,2,3,4)  .map(x => {    if(x === 3) {      throw 'I hate threes';    }    return x;  })  .retryWhen( errs => errs.delay(1000).take(3))  .subscribe(    x => console.log(x),    err => console.error(err),    () => console.info('done')  );/*12121212"done"*/

This it goes to done, because the retryWhen run successfully, so we can concat and error to make it goes to error block:

Observable.of(1,2,3,4)  .map(x => {    if(x === 3) {      throw 'I hate threes';    }    return x;  })  .retryWhen( errs => errs.delay(1000).take(3)              .concat(Observable.throw("Go error")))  .subscribe(    x => console.log(x),    err => console.error(err),    () => console.info('done')  );/*12121212"Go error"*/

 

转载地址:http://qptkm.baihongyu.com/

你可能感兴趣的文章
安卓手机当Transmission下载机、FTP、要点总结
查看>>
移动端无缝滚动兼拖动插件
查看>>
PyQt5学习笔记-从主窗体打开一个子窗体
查看>>
English 好的报纸
查看>>
CMS 01
查看>>
NSValue&NSNumber
查看>>
Bootstrap 常用组件汇总
查看>>
python 系统设置
查看>>
北京汽车官网经销商信息抓取(解析html标签)
查看>>
mysql学习之路五(转)
查看>>
Beyond Compare比较表格小技巧
查看>>
第2章 理解面向对象
查看>>
数组的声明和遍历
查看>>
Mouse Key Hook
查看>>
Scrapy框架基础使用
查看>>
python学习笔记-(一)初识python
查看>>
前端的事件流以及事件处理程序
查看>>
react中create-react-app详情配置文档
查看>>
TLD单目标跟踪算法程序详解--OpenTLD Code 详解
查看>>
PDO基础知识
查看>>