Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining results when using switchMap and forkJoin

Tags:

angular

rxjs

I have an http service which returns an object array, and by using this results am calling another service as given in the example. Now how can I combine the intermediate and final results at the end. For eg: result1Data[0] + result2Data[0] etc..

var result1= this.service1.getData();

result1.pipe(
  switchMap(data => {
    let result2 = data.map(result1Data => {
      return this.service2.getData(result1Data);
    });
    return forkJoin(...result2);
  })
).subscribe(result2Data => {
   
 //combine result1Data and result2Data
 
})
like image 671
Jish Nair Avatar asked Jun 09 '26 00:06

Jish Nair


1 Answers

Here's how it could be implemented:

var result1 = this.service1.getData();

result1.pipe(
  switchMap(result1Data => {
    return forkJoin(result1Data.map(this.service2.getData))
      .pipe(
        map(result2Data => {
          //combine result1Data and result2Data ()
        })
      )
    )
  })
).subscribe(result3 => {

  // allready combined

})
like image 171
Rafi Henig Avatar answered Jun 10 '26 15:06

Rafi Henig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!