3
votes

Am new to the rxjs and i would like to return an observable of either true or false

this is what i have tried

checkLoggedin():Observable<boolean> {
  //check from server if user is loggdin
  if(this._tokenService.getToken()){ //this returns synchronous true or false

      this._httpservice.checkifloggedin()
         .subscribe((res)=>{
            return res.data //this comes in as true or false value
              },
              err=>{ return false }
              )    
     }else{

        return false  //this fails with an error of 
                    //type false is not assignable to observable<boolean>
        }

 }

How do i change the above else part to work with the boolean observable so that in the other components i can only do

this._authservice.checkLoggedin()
  .subscribe.....//here get the value whether true or false
2

2 Answers

5
votes

this should work

checkLoggedin():Observable<boolean> { 
  if(this._tokenService.getToken()){ 
    return this._httpservice.checkifloggedin().map(res=>res.data ) ;
  } else { 
    return Observable.of(false);
  }
}
3
votes

Use return Observable.of(false) instead of return false.