如何在Angular中失败时继续合并映射



当一个mergeMap失败时,我似乎不知道如何在Angular中继续使用它。

我把两个mergeMap调用串在pipe内部的一个可观察对象上。两者都完成了所有调用,但第二个合并映射调用中的一个返回错误,因此第二个归并调用中没有一个得到映射。

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);
})
)),
);

当然,第二CCD_ 5需要能够处理第一CCD_ 7返回的CCD_。

最新更新