0

Why my alert is being thrown before I can print console.log("outside the .then" + this.result);? The result is true, my this.result = Admin_product

canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    this.oktaAuthService.getUser().then(
      (res) => {
        this.result = res.groups.find((group) => group === 'Admin_product');
        console.log("inside the .then" + (this.result))
      }
    )
    if (this.result === 'Admin_product') {
      console.log("outside the .then" + this.result);
      return true;
    } else {
      alert('User is not authorized to perform this operation');
      return false;
    }

  }
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 4
    You should not have expected otherwise, you don't _await_ the promise. Read the canonical https://stackoverflow.com/q/14220321/3001761. – jonrsharpe Mar 28 '22 at 20:41
  • ah thank you for your response I do not have much knowledge about promises – Anna Lanna Mar 28 '22 at 20:46
  • 1
    The promise callback (in the `then(...)`) is exectued after the synchronous part of the function is ended, so after your `alert` and `return false`. – Peterrabbit Mar 28 '22 at 20:47

0 Answers0