angular - 如何在 Angular 6(使用 rxjs 6)中订阅多个 HTTP 请求并确保发出所有请求,即使其中一个会引发错误
问题描述
我有三个 observables,由 HttpClient 的 get() 方法返回。我使用 forkJoin 运算符是为了在所有请愿结束时获得信号。
const responses = [
this.http.get('http://localhost:3000/articles').pipe(
map( data => this.articles = data ),
catchError( err => throwError("Can't get the articles list") )
),
this.http.get('http://localhost:3000/notas').pipe(
map( data => this.news = data ),
catchError( err => throwError("Can't get the news list") )
),
this.http.get('http://localhost:3000/users').pipe(
map( data => this.users = data ),
catchError( err => throwError("Can't get the users list") )
)
];
forkJoin(responses).pipe(
catchError( err => throwError(err) )
).subscribe(
response => console.log('All data loaded')
);
如果第一个 HTTP 请求抛出错误,则发出其他 HTTP 请求,但不执行相应的回调。我在rxjs 文档上读到“如果在某些时候有任何输入 Observable 错误,forkJoin 也会出错,并且所有其他 Observable 将立即取消订阅。”。所以我猜我的 forkJoin 正在按预期工作,但是我没有得到我需要的功能。
那么:我如何使用 forkJoin 或任何等效方法确保发出所有请求并执行所有回调,即使其中一个抛出错误?
解决方案
我终于管理它创建了一个自定义的可观察对象,当所有内部可观察对象都发出或抛出错误时发出:
public allRequests(requests: Observable<any>[]): Observable<any> {
return new Observable(
observer => {
requests.forEach((request, i) => {
request.subscribe(
resp => {
emitOnLast(i);
},
err => {
emitOnLast(i);
console.error(err);
}
);
const emitOnLast = (n: number) => {
if (n === requests.length - 1) {
observer.next(this.msg);
}
};
});
}
);
}
推荐阅读
- r - 用 R 复制 Excel 文件
- android - 如何从第三方应用程序打开 Waze 的语音识别?
- asp.net-mvc - 使用 Asp.Net Core 中的数据保存多个图像
- c++ - 迭代 const 容器时编译器会展开“for”循环吗?
- android - 运动布局动画不播放
- google-apps-script - Googlesheets - 从函数结果设置单元格格式
- javascript - Javascript 时区 gmt 和偏移量
- python - 将 scipy.ndimage.rotate 与 alpha 通道一起使用时图像中的伪影
- r - 如何在R中的另一个字符串的重复间隔处插入一个字符串
- sql - 在用特殊字符分隔的表中拆分列值