What is the correct way to write this without using a nested subscription?
The first observable will return a value that is passed to the second observable, then I need to use the returned values from both observables in the final subscription.
Also, the "doStuff" method is called from a button click, do I need to also unsubscribe from these observables to prevent memory leaks?
doStuff() {
// Get app user
this.appUserService.getAppUser(true).subscribe((appUser) => {
// Get login key
this.accountApi.getLoginKey(appUser).subscribe(loginKey => {
// Open in app browser
this.browser = this.inAppBrowser.create(appUser.Url + loginKey, '_system');
});
});
}
Edit 1
Tried the following as suggested but this does not compile, on the second pipe there is the following error:
Property 'pipe' does not exist on type 'OperatorFunction<AppUser, any>'
Then within the "map" function there is the following error:
Cannot find name 'appUser'.
return this.appUserService.getAppUser(true).pipe(
switchMap((appUser: AppUser) => this.accountApi.getUserLoginKey(appUser)).pipe(
map(loginKey => { appUser, loginKey; }),
),
tap(({ appUser, loginKey }) => {
this.browser = this.inAppBrowser.create(appUser.Url + loginKey, '_system');
}),
take(1)
);
Edit 2
I have now updated my solution as follows which works (but I could argue my original solution did work), so I have removed the nested subscriptions but they have been replaced with nested pipes instead. I believe this is a more acceptable solution (i.e. using nested "pipes" rather than nested "subscriptions") but not sure if it can be improved any further?
doStuff() {
// Get app user
this.appUserService.getAppUser(true).pipe(
switchMap(appUser => this.accountApi.getLoginKey(appUser).pipe(
tap(loginKey => {
// Open in app browser as return URL
this.browser = this.inAppBrowser.create(appUser.Url + loginKey, '_system');
})
)),
take(1)
).subscribe();
}