Sometime we want to set a default or fallback value when network request failed.

http$
.pipe(
map(res => Object.values['payload']),
shareReplay(),
catchError(err => of([])) // return an empty value
);

Sometime we want to just throw the error again:

http$
.pipe(
catchError(err => {
return throwError(err)
}), // put catchError and finalize to make sure they will be only run once
finalize(() => console.log('Finalize executed...'))
map(res => Object.values['payload']),
shareReplay()
);

You know about the finally instruction in the try-catch? What if you wanted to have the same when using RxJS Observables? Surprise, there's actually an RxJS operator :wink: called finalizewhich can be used for exactly that. Let's explore in more details.

private execCall(endpoint: string) {
this.isLoading = true;
this.http.get(`/assets/${endpoint}`)
.pipe(
tap(x => console.log('tap', x)),
catchError(err => console.log('did throw') || throwError(err)),
finalize(() => {
this.isLoading = false;
console.log('finalize');
})
)
.subscribe(x => {
console.log('Got result', x);
}, (err) => {
console.error('Got error', err);
})
} /**
did throw
Got error
Error: operators_1.throwError is not a function
finalize
*/

[RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator的更多相关文章

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

  2. [RxJS] Error handling operator: catch

    Most of the common RxJS operators are about transformation, combination or filtering, but this lesso ...

  3. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

    The ideal time to catch an error is at compile time, before you even try to run the program. However ...

  4. Erlang error handling

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

  5. Error Handling

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

  6. Error Handling and Exception

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

  7. Clean Code–Chapter 7 Error Handling

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

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

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

  9. Fortify漏洞之Portability Flaw: File Separator 和 Poor Error Handling: Return Inside Finally

    继续对Fortify的漏洞进行总结,本篇主要针对 Portability Flaw: File Separator 和  Poor Error Handling: Return Inside Fina ...

随机推荐

  1. ACM_小游戏(棋盘博弈)

    Problem Description: 最近kiki无事可做,于是他想玩棋盘游戏.棋盘的大小是n * m.首先,棋子放置在右上角(1,m). 每次可以将棋子向左方,下方或左下方移动一个位置.当移动到 ...

  2. Android从Camera中获取图片的两种方法

    方法一: 此方法会由Camera直接产生照片回传给应用程序,但是返回的是压缩图片,显示不清晰 ? 1 2 3 4 5 6 try {      Intent cameraIntent = new In ...

  3. Hadoop Hive概念学习系列之hive里的扩展接口(CLI、Beeline、JDBC)(十六)

    <Spark最佳实战  陈欢>写的这本书,关于此知识点,非常好,在94页. hive里的扩展接口,主要包括CLI(控制命令行接口).Beeline和JDBC等方式访问Hive. CLI和B ...

  4. 自动保存草稿 asp+ajax自动存稿功能详解(转自影子)

    自动保存草稿功能的原理 我们都知道网页是一种无状态的,每次都需要请求,响应,当一次请求完成后就与服务器断开连接了,所以我们不能像网页一样实现实时的交互功能,但是为了满足更多的需求一个比较无敌的程序员还 ...

  5. JS高级——Function

    Function构造函数 可以用来新建函数对象 1.一个参数都不传的情况创建的就是一个空的函数 2.只传一个参数的情况这个参数就是函数体 3.传多个参数的情况,最后一个参数为函数体,前面的参数都是该函 ...

  6. JS——样式类的添加

    1.注意current前有个空格 this.className = this.className + " current"; 2.直接将class所有的值替换成current th ...

  7. [Windows Server 2003] 安装PHP+MySQL方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:PHP+MyS ...

  8. CentOS下使用yum安装配置和使用svn

    安装说明 系统环境:CentOS-6.3安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 检查已安装版本 ? 1 2 3 4 5 6 7 8 9 1 ...

  9. JS DOM节点(当前标签和同级、父级、子级..之间的关系)

    1. 通过顶层document节点获取    1) document.getElementById(elementId) //根据id获得    2) document.getElementsByNa ...

  10. IntentService和HandlerThread的使用以及源码阅读

    使用MyIntentService.java public class MyIntentService extends IntentService { /** * 是否正在运行 */ private ...