Subscribe to multiple Observables (like chaining then() in Promises)

前端 未结 2 1377
小鲜肉
小鲜肉 2020-12-25 08:52

My Angular 2 application has 2 methods (GetCategories() and GetCartItems()) in a service , and both of these methods return Observable

2条回答
  •  离开以前
    2020-12-25 09:21

    Looks like GetCartItems doens't depend on GetCategories. Then you can use zip:

    Observable
        .zip(
            this.appService.GetCategories()
            this.appService.GetCartItems()
        )
        .catch(err => this.toaster.error(err))
        .subscribe(([categories, cartItems]) => {
            this.appService.categories = categories;
            this.appService.cart = cartItems;
        });
    

提交回复
热议问题