Avoiding nesting subscriptions in Angular 2+?

后端 未结 3 974
青春惊慌失措
青春惊慌失措 2020-11-30 14:59

I have 2 endpoints:

  • 1 endpoint to get current user logged.
  • 1 endpoint to get grants of this user.

Actually I use:



        
3条回答
  •  攒了一身酷
    2020-11-30 15:52

    In your code it looks like they have no dependency on each other, because the result from this.user is not used in this.grants.getbut I'll ignore that for now.

    You can use the await keyword to prevent nesting.

    For example you could do the following:

    const user  = await this.user.toPromise();
    const grants = await this.grants.get().toPromise();
    

提交回复
热议问题