[RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator
Error The and Rxjs Operator catch Handling Strategy
2023-09-14 08:59:17 时间
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 finalize
which 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 */
相关文章
- [Android] The connection to adb is down, and a severe error has occured
- MySQL 从 5.5 升级到 5.6,启动时报错 [ERROR] Plugin 'InnoDB' init function returned error
- 【异常】The dependencies of some of the beans in the application context form a cycle
- Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
- Error -27492: "HttpSendRequest" failed, Windows error code=12152 (invalid server response) and retry。。。
- innobackupex: Error: The xtrabackup child process has died at /usr/bin/innobackupex line 2672
- 随笔 - 141 文章 - 2 评论 - 289 Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法
- [RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator
- [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable
- Error -27492: "HttpSendRequest" failed, Windows error code=12152 (invalid server response) and retry。。。
- Unexpected XML declaration. The XML declaration must be the first node in the document and no white
- Fiori Error message Exception raised without specific error
- 成功解决The following specifications were found to be incompatible with the existing python installation
- The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0
- The difference between the request time and the current time is too large.阿里云oss上传图片报错
- SVN Error: “' 'x' isn't in the same repository as 'y' ” during merge (并不在同一个版本库中)
- [TroubleShootin]The backup set holds a backup of a database other than the existing 'xxdb' database.
- chroumium electron apply_all_patches.py patch git error WindowsError: [Error 2] The system cannot find the file specified
- 解决harbor仓库拉镜像报错ErrImagePull: rpc error: code = Unknown desc = Error response from daemon: repository
- Error:The supplied javaHome seems to be invalid. I cannot find the java executable. Tried location:
- (六)Jenkins部署项目报错The username you provided is not allowed to use the text-based Tomcat Manager (error
- Repo报错:GnuTLS recv error (-9): Error decoding the received TLS packet.