简体   繁体   中英

Angular: wait for Observable inside a function before returning result

I need help with the following function:

  getUser(uuid: string): Observable<WowUserDataModel> {
    let user: WowUserDataModel = {
      login: null,
      userUuid: uuid,
      firstName: null,
      lastName: null,
      displayName: null,
      email: null,
      authorities: null
    };

    return this.http.get(WowUrlProvider.gateway.wowUserAuthorities.replace('{userUuid}', uuid.toUpperCase())).pipe(
      map((authorities) =>
        user.authorities = authorities,
      )
    );
  }

What I need: to wait for the this.http.api call (which returns Observable) to complete, assign the result to user.authorities, and only then return Observable of the entire user object (always a single user)

What is happening now: the function returns just authorities property, not the whole user object

Can this be achieved without changing the return type of the function?

getUser(uuid: string): Observable<WowUserDataModel> {
  // ...
    return this.http.get(WowUrlProvider.gateway.wowUserAuthorities.replace('{userUuid}', uuid.toUpperCase())).pipe(
      map((authorities) => {
       user.authorities = authorities;
       return user;
      })
    );
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM