Я работаю с http-запросами, «инкапсулированными» как сервисы в моем js-коде.
Изначально мой код был немного спагетти, используя этот способ:
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);
}
});
Этоработает, но противно. Поэтому я переписал все утверждение так:
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();
Это лучше, но я думаю, что есть еще лучший способ сделать это, просто я этого не знаю.
Я не прав?