I have the following code which consists of multiple subscribes. What I need to achieve is like this :
- Subscribe to activatedRoute to get User and Product data.
- With the product data returned, subscribe to getSeller service by using the product data.
- Subscribe to getRating service by using the seller data returned.
My question : is there any better way to perform these nested subscription? is it a good practice to do like this?
this.activatedRoute.data.pipe(
map((data) => {
this.user = data['user'];
this.product = data['product'];
return this.product;
})
).subscribe(result => {
if (this.product === null) {
this.router.navigate(['/home']);
} else {
this.displayCurrency = this.dataService.getCurrencySymbolById(this.product.currency);
this.userService.getUser(this.product.createdBy).subscribe(seller => {
this.seller = seller;
this.ratingService.getRatingByUserId(seller.id).subscribe(rating => {
this.rating = rating;
})
});
}
});
See Question&Answers more detail:os