How to execute something after subscribe in angular

Viewed 33920

I want to return a boolean value ,but the variable in the "if" condition is undefined.

function() {
    this.menuDataService.getMenu()
      .subscribe(res => {
       this.mainMenus = res.MainMenus;
       console.log(this.mainMenus);
    });

    console.log(this.mainMenus);

    if(this.mainMenus == 1){
       return true;
    }
    else {
      return false;
    }
}
3 Answers

seescode's answer helped me but I can't comment it.

I just wanted to say that with RxJS 6, their way to chain Observable operators changed, and we now have to use the pipe(...) method. (see RxJS 6 - What Changed? What's New?)

Here is what his InnerFunc would look like updated to RxJS 6 :

function InnerFunc(){
  return this.menuDataService.getMenu().pipe(
    map(res => {
      if(res.mainMenus == 1){
       return true;
      }
      else{
       return false;
      }
    )
  )
}
Related