RxJS:mergeMap 运算符在其中一个请求抛出错误时取消其他活动请求

RxJS: mergeMap operator cancelling other active requests when one of them throws an error

基于article 我尝试通过对服务器的多个请求来实现并行更新逻辑:

const prouctIds: number[] = [1, 2, 3];
const requests = from(prouctIds)
    .pipe(mergeMap(id => /* Emulating error when updating some entity*/ throwError(id + '/err')));
requests.subscribe(success => console.log(success), err => console.error('error block', err));

正如作者所说:

So if there is any request fails i.e. throws server side error, then it doesn't affect other parallel requests.

但在我的情况下,如果某些请求 returns 出现错误,其他活动请求将被取消。如果您运行上面的代码片段,您只会在控制台中注意到一条错误消息。

  1. 我知道我可以将 forkJoin 与内部 catchError 管道一起使用,但是 如果 mergeMap 那么这些运算符之间的区别是什么 如果其中一个发生错误,也会取消并行请求?
  2. 如何在不使用内部 catchError 管道并将错误转换为常规可观察值的情况下实现我的目标,即 catchError(err => of(err));

提前致谢!

来自 RxJS 文档:

"Error" and "Complete" notifications may happen only once during the Observable Execution, and there can only be either one of them.

In an Observable Execution, zero to infinite Next notifications may be delivered. If either an Error or Complete notification is delivered, then nothing else can be delivered afterwards.
[RxJS Documentation - Observable]

所以你不能让 Observable 多次调用错误回调,正如你发布的文章所建议的那样。您必须使用内部 catchError 到 return 一个不会出错的 Observable。

如果您想在响应到达时立即获得响应,请使用 mergeMapmerge 而不是 forkJoin,因为 forkJoin 仅在所有内部 Observable 完成时发出。