如果concatMap中的请求获取失败,如何跳过元素
const obs = from(this.genes);
this.subs = obs.pipe(
concatMap(res => {
const gn = this.service.getData(this.resource + '/' + res.name);
return gn.pipe(map(r => {
for (let x = 0; x < res.l; x++) {
this.tableData[res.i + x]['g'] = r.gene;
}
}, err => {
console.log(err);
}));
})
).subscribe(x => {}
);
以下是我的数据
[
{
"name": "tim",
"i": 0
},
{
"name": "keratin",
"i": 1
}...
如果第一个URL失败,则不会使用下一个URL。如何跳过?
答案 0 :(得分:0)
concatMap()
仅影响可观察到的源的“成功”发射(“ next()
处理程序称为”)。此外,对于“跳过”值,您无法为其定义处理程序。
我将执行以下两个操作之一:
this.subs = obs
.pipe(
concatMap(res => this.service.getData(this.resource + '/' + res.name),
// result selector to make both variables avaiable in subscribe
(outer, inner) => ({ res: outer, data: inner }))
)
.subscribe({
next: (response => {
for (let x = 0; x < response.res.l; x++) {
this.tableData[response.res.i + x]['g'] = response.data.gene;
}
}),
error: err => console.error(err)
})
OR
this.subs = obs
.pipe(
concatMap(res => {
return this.service.getData(this.resource + '/' + res.name)
.pipe(
tap(r => {
for (let x = 0; x < res.l; x++) {
this.tableData[res.i + x]['g'] = r.gene;
}
})
)
})
)
.subscribe({
next: response => { },
error: err => console.error(err)
})