【问题标题】:How to have a separate error handling for each function in combineLatest?如何对 combineLatest 中的每个函数进行单独的错误处理?
【发布时间】:2018-09-11 12:24:26
【问题描述】:

我需要在这个 combineLatest 中处理每个函数的错误场景。我将在下面添加代码

const combined = combineLatest(
  this.myservice1.fn1(param),
  this.myservice2.fn2(), 
  this.myservice3.fn3());

const subscribe = combined.subscribe(
  ([fn1,fn2, fn3]) => {
    // operations i need to do
  },
  // how to handle error for fn1, fn2, fn3 separately
  (error) => {
  }
);

任何帮助将不胜感激!

【问题讨论】:

    标签: angular error-handling rxjs combinelatest


    【解决方案1】:

    您可以在使用 combineLatest latest 之前捕获每个源 Observable 的错误,如果您想稍后处理它们或将它们转换为不同的错误,则最终重新抛出它:

    combineLatest(
      this.myservice1.fn1(param)
        .pipe(
          catchError(err => {
            if (err.whatever === 42) {
              // ... whatever
            }
            throw err;
          }),
        ),
      this.myservice2.fn2()
        .pipe(
          catchError(err => /* ... */),
        ),
      ...
    )
    

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2018-03-29
      • 2022-01-21
      • 1970-01-01
      • 2018-06-23
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多