Angular 失败时如何继续 mergeMap

How to continue a mergeMap when one fails in Angular

我似乎不知道如何在 Angular 中继续 mergeMap 失败。

我将两个 mergeMap 调用串在一起,调用 pipe 中的一个可观察对象。两者都完成了所有调用,但第二个合并映射调用之一 return 出错,因此 none 第二个合并调用被映射。

Observable.pipe(
   mergeMap(
     result => {
       return stuff
       });
     }
   ), mergeMap(result => {
     return stuff //one of the calls here will error, but the rest work
   }),
   catchError(error => {
     return of(undefined)
   })
 ).subscribe({
   next: (result) => {
     //this will be undefined if one of the above calls failed, works otherwise
     if(result != undefined)
   { 
   //stuff
   }

我对此还是很陌生,所以任何帮助将不胜感激。

在将错误传播到主链之前,您需要将 catchError() 与嵌套的 Observable 链接起来。

source$.pipe(
  mergeMap(result => stuff.pipe(
    catchError(error => {
      return of(undefined);
    }),
  )),
  mergeMap(result => stuff.pipe(
    catchError(error => {
      return of(undefined);
    })
  )),
);

当然,第二个 mergeMap 需要能够处理第一个 mergeMap 返回的 undefined