Reputation: 420
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
Upvotes: 6
Views: 2391
Reputation: 14385
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(...)
Upvotes: 12
Reputation: 5224
Not sure this is the best way to do it, but here is a way to do it :
this.session.account.get()
.switchMap(account => zip(Observable.from(account), this.employerApi.get(account.accountId))
.switchMap((account, employer) => zip(Observable.from(account), this.addressApi.get(employer.addressId))
.filter((account,address) => address.numer % 2)
.subscribe((account,address) => console.log(¿¿¿¿¿account.name?????, address.name));
In terms of best practices however, I think what you need is a serializer in the backend that returns the results in a json format, something like : { "account": account_object, "address" : address_object}
.
Upvotes: 1