【问题标题】:what's the antonym for catchError in rjxs?rxjs 中 catchError 的反义词是什么?
【发布时间】:2019-02-07 20:55:24
【问题描述】:

我的拦截器中有以下代码。

return next.handle(clonedReq).pipe(
            catchError( (error):any =>  {
                console.log(error);
                this.loaderService.storeLoaderOff();
            }), tap( (success)=>{
                console.log(success);
               this.loaderService.storeLoaderOff();
            })
        );

但我还需要 onSuccess 管道,以便如果它返回成功,我会做其他事情。

我找不到类似的东西。也许你们有过这方面的经验。

【问题讨论】:

  • 也许你可以使用tap
  • 它有效。所以在tap管道中,它会在http返回成功而不是错误时触发,对吧?
  • 我不能 100% 确定。所以我只是在谷歌上搜索它。
  • 是的,点击就是要走的路!
  • 我也不确定,但我认为您可以使用带有 3 个回调(参数)的 tap 而不是 catchError。第一个回调是whatever,第二个回调是error,第三个回调是complete

标签: angular ionic-framework rxjs reactive-programming


【解决方案1】:

Tap 让您可以监听并获取一个成功处理程序,然后是一个可选的错误处理程序

const { throwError, of } = rxjs;
const { tap } = rxjs.operators;

throwError('This is the error').pipe(
  tap(
    res => { console.log(res); },
    err => { console.log(err); }
  )
).subscribe(_ => {}, _ => {});

of('This is the response').pipe(
  tap(
    res => { console.log(res); },
    err => { console.log(err); }
  )
).subscribe(_ => {}, _ => {});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.4.0/rxjs.umd.min.js"></script>

【讨论】:

    猜你喜欢
    • 2011-01-04
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多