//我有一个数组,我使用foreach循环并进行http post调用。事情是我需要等待成功/错误响应并触发第二个http调用,然后从数组索引中触发第三个。我尝试使用forkJoin,但它在数组本身的末尾触发响应。有人可以帮我解决这个问题吗?
我的示例代码如下所示:
let cars= [
{
color:'red',
brand:'honda'
},
{
color:'white'
brand:'audi'
}
];
//Component
let observables = cars.map(car => this.carService.getCarByBrand(car));
// forkJoin the array/collection of observables
let source = Observable.forkJoin(observables);
source.subscribe(x => console.log(x);
//Component
//trigerring point
return this.carQuestion().do(response => {
selectedCar = response;
});
private carQuestion(){
let observables = cars.map(car =>
this.carService.carSelection(this.language, car));
let source = concat(...observables);
source.subscribe(x => console.log(x));
}
//carService
//carSelection should trigger on each call and wait for the response
carSelection(language: string, car){
//post call where it opens modal. Note if i send a single response
// without the cars loop it works fine
return this.inputService.getInput(
inputService.mapper,
).flatMap(response => {
if (response.yes) {
return Observable.of({
selectedCar: response.data,
});
}
else {
return Observable.of(null);
}
}).first();
}
谢谢!