Bird
0
0

Examine this canActivate guard implementation:

medium📝 Debug Q6 of 15
Angular - Routing
Examine this canActivate guard implementation:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
  return Promise.resolve('true');
}

What is the issue with this code?
AIt returns a Promise resolving to a string instead of a boolean
BIt should return an Observable instead of a Promise
CThe method signature is missing the return type
DIt should return a string, not a Promise
Step-by-Step Solution
Solution:
  1. Step 1: Check return type

    canActivate can return boolean, Observable<boolean>, or Promise<boolean>.
  2. Step 2: Analyze returned value

    The Promise resolves to the string 'true', not the boolean true, which is invalid.
  3. Step 3: Correct usage

    Return Promise.resolve(true) (boolean) instead of Promise.resolve('true') (string).
  4. Final Answer:

    It returns a Promise resolving to a string instead of a boolean -> Option A
  5. Quick Check:

    Return types must match boolean, not string [OK]
Quick Trick: canActivate must return boolean or Promise/Observable of boolean [OK]
Common Mistakes:
MISTAKES
  • Returning string 'true' instead of boolean true
  • Ignoring return type requirements
  • Assuming any Promise return is valid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes