1

//I had an array which i'm using a foreach to loop and make a http post call. Thing is that i need to wait for the success/error response and trigger the second http call and then third from the array index. I tried using forkJoin but it triggers response at the end of the array itself. Can some help me resolve this please?

My sample Code looks like as follows:

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();
}

Thanks!

rUI7999
  • 129
  • 1
  • 3
  • 20

0 Answers0