我有一段代码,在这段代码中,我为每个ID发起一个ajax请求,并在结果返回时对它们进行处理。这是我实际代码的简单复制品和jsfiddle:
var ids = [1,2,3,4,5,6];
var ids$ = (id) => {
return Observable.of(id);
};
var loadIds$ = (id) => {
if(id == 4) return xhr('/echo/jsoneee/', {id: id});
return xhr('/echo/json/', {id: id});
};
Observable.from(ids)
.concatMap(id => Observable.forkJoin(ids$(id), loadIds$(id)))
.catch((err, caught) => {
//get the id for which loadIds failed
//send the id to subscribe() and continue the subscription
return Observable.empty();
})
.subscribe(result => console.log(result))
现在我需要修改代码,如果发生错误,我需要获取ajax请求失败的id
,然后就像什么也没发生一样继续订阅。但我还没有能够做到这一点。非常感谢帮助。
.concatMap(id => Observable.forkJoin(ids$(id), loadIds$(id)))
- martinforkJoin
或zip
,但目的是subscribe()
应该将id
对象和该id
ajax 请求的响应作为单个项目一起传递,因为我在 subscribe 中进行其他处理,我需要这两个数据。 - lbrahim