In a previous post I discussed taking two observables and joining their data together in one array. But what do you do if the output one observable is required in another? The below uses two switchMaps and some destructing to propagate and use the data from the first observable, collecting new data along the way and ultimately making it available in the subscribe(). As usual, this needs error checking an validation but gives the idea.
this.sub = this.accountService.getAccountInformationObservable().pipe(
switchMap(user => {
return this.secondService.getSecondServiceData(user).pipe(
map(secondData => ({user, secondData}))
);
})
).pipe(
switchMap(({user, secondData}) => {
return this.thirdService.getThirdServiceData(secondData).pipe(
map(thirdData => ({user, secondData, thirdData}))
);
})
).subscribe(({user, secondData, thirdData}) => {
this.user = user;
this.secondData = secondData;
this.thirdData = thirdData;
});