【发布时间】:2022-01-03 17:56:59
【问题描述】:
我将 2 个 api 服务调用压缩到一个 observable 中:
const firstAPIrequest = this.firstApi.getData();
const secondAPIrequest = this.secondApi.getData();
const combined = zip(firstAPIrequest, secondAPIrequest);
我目前映射这些以返回一个数组:
combined.pipe(
map((res) => {
return [res[0], res[1]) // just an example
}
);
然后我将它用作不同组件中的订阅
combined.subscribe(
(res) => this.handleData(res),
(err) => {
console.debug('Fetch failed', err);
}
)
在这种情况下,如果其中一个 API 请求失败,它会显示错误,但我想检查其中一个失败,例如;如果request1 返回数据并且request2 失败,我仍然会从request1 返回数据
我尝试过catchError,但我认为我无法获取此函数中的数据
combined.pipe(
catchError((err) => ...), // how would I catch the data before it throws an error?
map((res) => {
return [res[0], res[1]) // just an example
}
);
【问题讨论】:
-
您希望处理错误还是完全忽略它们?
-
@MichaelD 不要完全忽略它们,而是如果两者都失败抛出错误,如果只有一个失败而另一个工作它不应该抛出
标签: angular error-handling rxjs observable rxjs6