Search code examples
angularrxjsrxjs6

Angular RxJS forkJoin switchMap return observable of boolean


Hi I've written an function and I want to return Observable of boolean

public loadUser(): Observable<boolean> {
    return forkJoin([this.getUser(), this.getUserEntitlements()]).pipe(
      switchMap(([userId, userEntitlements]) => {
        const consolidatedResp = {
          'userId': userId,
          'userEntitlements': userEntitlements
        }
        this.userId = consolidatedResp.userId.toLowerCase();
        this.processUIEntitlements(userEntitlements);
        if (consolidatedResp.userEntitlements.firstName) {
          return of(consolidatedResp);
        } else {
          return this.getUserDetails(this.userId)
        }
      }),
      map((userDetails: IUserInfo | { userId: string, userEntitlements: ICurrentUser }) => {
        if (userDetails) {
          if (!('userEntitlements' in userDetails)) {
            this.userInfo = userDetails;
            this.setUserName();
          }
          return of(true)
        } else {
          return of(false)
        }
      })
    )
  }

but in the return forkJoin line it displays an error saying Type 'Observable<Observable<boolean>>' is not assignable to type 'Observable<boolean>'. I'm returning observables in map, is that the case?


Solution

  • That's because your map() returns an observable, yes.

    Return a boolean directly or replace you map with another switchMap if the returned value is any observable