My Angular 2 application has 2 methods (GetCategories()
and GetCartItems()
) in a service , and both of these methods return Observable
s.
In order to invoke these two methods one after another from my component, I have written below 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)}
})
});
}
Basically, calling GetCartItems()
from within subscribe()
of GetCategories()
, and I feel this is NOT the right approach. This is kind of callback hell.
Any idea on how to implement this in a better way (like chaining then()
in Promise
s)?