Search code examples
angulartypescriptobservable

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?


Solution

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