My Angular 2 application has 2 methods ( GetCategories() and GetCartItems() ) in the service, and both of these methods return Observable s.
To call these two methods one after another from my component, I wrote the following code:
ngOnInit() { this.appService.GetCategories().subscribe( (data) => { this.appService.categories = data; this.appService.GetCartItems().subscribe( { next: (data) => { this.appService.cart = data}, error: (err) => { this.toaster.error('cart==>' + err)} }) }); }
Essentially, calling GetCartItems() from GetCartItems() subscribe() the GetCategories() function, and I believe this is NOT the right approach. This is a kind of callback to hell.
Any idea on how to implement this better (e.g. then() chain in Promise s)?
source share