4

我需要在这个 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) => {
  }
);

任何帮助,将不胜感激!

4

1 回答 1

14

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

combineLatest(
  this.myservice1.fn1(param)
    .pipe(
      catchError(err => {
        if (err.whatever === 42) {
          // ... whatever
        }
        throw err;
      }),
    ),
  this.myservice2.fn2()
    .pipe(
      catchError(err => /* ... */),
    ),
  ...
)
于 2018-04-02T10:17:28.550 回答