I'm working with http requests "encapsulated" as services in my js code.
Initially, my code was a little bit spaghetti, using this way:
let user;
let business;
// check if the user is authenticated
authenticationService.isAuthenticated().subscribe(authenticated => {
// if is authenticated
if(authenticated) {
// get the user profile data and save it to the user variable
profileService.get().subscribe(data => user = data);
// get the user business data and save it to the business variable
businessService.get().subscribe(data => business = data);
}
});
This is working, but nasty. So I rewrote the whole statement as
authenticationService.isAuthenticated()
.pipe(
filter(a => a), // continue only when true
tap(() => {
// as above, get data and save to relative variables
this.profileService.get().subscribe(data => user = data));
this.businessService.get().subscribe(data => business = data));
})
).toPromise();
This is better, but I think there is a even better way to do this, just I don't know it.
Am I wrong?