除了使用不同的参数外,我多次查询单个 API 端点。无论出于何种原因,其中一些请求可能会失败并返回 500 错误。如果他们这样做了,我仍然希望其他请求继续进行,并将所有成功请求的数据返回给我。
let terms = [];
terms.push(this.category.category);
terms = terms.concat(this.category.interests.map((x) => x.category));
for (let i = 0; i < terms.length; i++) {
const params = {
term: terms[i],
mode: 'ByInterest'
};
const request = this.evidenceService.get(this.job.job_id, params).map((res) => res.interactions);
this.requests.push(request);
}
const combined = Observable.forkJoin(this.requests);
combined.subscribe((res) => {
this.interactions = res;
});
最佳答案
最容易将每个请求与仅发出 null
的 catch
链接起来:
const request = this.evidenceService.get(...)
.map(...)
.catch(error => Observable.of(null)); // Or whatever you want here
失败的请求在 forkJoin
发出的结果数组中只有 null
值。
请注意,您不能在这种情况下使用 Observable.empty()
,因为 empty()
不会发出任何内容,只是在 forkJoin< 时完成
要求所有源 Observable 至少发出一个值。
关于Angular - RxJs ForkJoin 如何在出错后继续多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50129965/