Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rxjs switchmap observable, save variables for final

I need know a good practise for this problem.

I am calling to three services concat with switchMaps, but I need a variable of first service for the last service. How was the best practice to do this?

Example:

this.session.account.get()
.switchMap(account => this.employerApi.get(account.accountId))
.switchMap(employer => this.addressApi.get(employer.addressId))
.filter(address => address.numer % 2)
.subscribe(address => console.log(¿¿¿¿¿account.name?????, address.name));

Thanks for your help

like image 258
srhuevo Avatar asked Sep 02 '26 05:09

srhuevo


1 Answers

The simplest way would be to aggregate the values as you go:

this.session.account.get()
  .switchMap(account =>
    this.employerApi.get(account.accountId).map(employer => ({employer, account}))
  .switchMap(data => 
    this.addressApi.get(employer.addressId).map(address => ({...data, address}))
  .filter(data => data.address.number % 2)
  .subscribe(...)
like image 69
Meir Avatar answered Sep 04 '26 20:09

Meir