How come Angular http.post invoked without `.subscribe`?

58 Views Asked by At

Looking at this authguard which is called from canactivate :

@Injectable()
export class AuthGuard implements CanActivate {
    constructor(private loginServicePOST:LoginService, private router:Router) { }

    canActivate(next:ActivatedRouteSnapshot, state:RouterStateSnapshot) {
        return this.loginServicePOST({...}).map(e => {
            if (e) {
                return true;
            }
        }).catch(() => {

            return Observable.of(false);
        });
    }   
}

This code is working and an http request is invoked to server.

Question :

This is a cold observable and no one .subscribes to it - so I don't understand how this post request is invoked and why.
subscribe must be written IMHO.

NB
I already know that canactivate can return bool/Promise<bool>/Observable<bool>

1

There are 1 best solutions below

1
On BEST ANSWER

The router is subscribing to the observable returned by canActivate which invokes the observable returned by loginService(...).map(...)