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. 微信小程序的wxml文件和wxss文件在webstrom的支持

    webstrom默认不支持wxml文件和wxss文件,所以要进入设置里面手动添加支持. 对wxml文件的支持: 文件 -> 设置 -> 编辑器 -> 文件类型, 然后选择XML文件, ...

  2. 最大正方形 同luogu1387

    这道题下面这么写就够了(n<=100)暴力,枚举 #include<bits/stdc++.h> #define ULL unsigned long long #define MAX ...

  3. android 虚拟机,文件导入sdcard下报错,Read-only file system

    解决方案-------------------- eclipse -> windows->Android AVD Manager 里选择你的AVD,edit里SD Card 选择File, ...

  4. mysql数据库存储的引擎和数据类型

    一.查看支持的存储引擎 SHOW ENGINES \G; 或者 SHOW VARIABLES LIKE 'have%'; 二.安装版mysql的默认引擎是InnoDB,免安装版默认引擎是MyISAM ...

  5. fcc 响应式框架Bootstrap 练习3

    class="container-fluid"控制宽度100% <div class="container-fluid"> <h3 class ...

  6. SQL练习题_用户购买收藏记录合并(拼多多)

    目录 拼多多笔试题0805_统计用户数据 笔试题描述 表格构建 数据观察 题目分析 一.合并表格 二.CASE表示(0,1) 三.同理复制FORK表 题目解答 拼多多笔试题0805_统计用户数据 笔试 ...

  7. jsp 文件下载

    有的时候一个模板的下载,这种简单的下载服务端已存在文件功能,就可以方便的通过jsp文件下载的方式来轻松实现. //jsp 页面 js /** * 导出角色 */ function exportRole ...

  8. PowerDesigner16逆向工程生成PDM列注释(My Sql5.0模版)

    一.编辑当前DataBase 选择DataBase——>edit Current DBMS...弹出如下对话框:  如上图,先解释一下: 根据红颜色框从上往下解释一下. 第一个红框是对应的修改的 ...

  9. JS 获得节点

    var ele = ev.parentNode; var elem_child = ele.childNodes; in elem_child) { //遍历子元素数组 if (elem_child[ ...

  10. js post一个对象 在C#中怎么接收? 未解决

    这个在C#中怎么接收?