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. NS2学习笔记(一)

    NS2有两种运行方式: 1.“脚本方式”,输入命令: ns tclscripl.tcl,其中 tclscripl.tcl 是一个Tcl脚本的文件名: 2“命令行方式”,输入命令:ns,进入NS2的命令 ...

  2. python--修改默认递归层级

    import sys sys.setrecursionlimit(最大递归次数)

  3. HDU 5279 分治NTT 图的计数

    思路: 显然每个子图内都是森林 去掉所有子图1和n都连通且每条大边都存在的情况 直接DP上 NTT优化一波 注意前两项的值.. //By SiriusRen #include <bits/std ...

  4. UNIX环境高级编程--2

    UNIX标准及实现 ISO C: 国际标准化组织(International Organization for standardization , ISO)ISO C标准的意图是提供C程序的可移植性, ...

  5. [转]linux grep命令

    转自:http://www.cnblogs.com/end/archive/2012/02/21/2360965.html 1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表 ...

  6. jdk11安装没有jre文件夹

    原因:jdk11安装之后是没有jre的 如果需要jre,需要到jdk目录下面去    打开命令窗口,然后执行如下命令:    bin\jlink.exe --module-path jmods --a ...

  7. 微信自定义分享功能实现Tips

    以MVC为例 前台js通过.post()方法传给后台特定Controller当前页面的url,后台获取后,进行处理: 1.获取access_token:https://mp.weixin.qq.com ...

  8. (转)Spring AOP的底层实现技术

    AOP概述 软件的编程语言最终的目的就是用更自然更灵活的方式模拟世界,从原始机器语言到过程语言再到面向对象的语言,我们看到编程语言在一步步用更自然.更强大的方式描述软件.AOP是软件开发思想的一个飞跃 ...

  9. 【译】x86程序员手册14-5.1段转换

    5.1 Segment Translation 段转换 Figure 5-2 shows in more detail how the processor converts a logical add ...

  10. [ECharts]"echarts/config" is not exists

    今天在给Echarts折线图中的数据点增加点击事件的时候总是出现一个 Uncaught Error: [MODULE_MISS]"echarts/config" is not ex ...