I want to create a function that will return true if http.get was successful, otherwise return false. I was trying something like that:
isOk() {
let isOk;
this.http.get('ip/api/isOk')
.subscribe(x => { isOk = true; },
(err) => { isOk = false; });
return isOk;
}
But it returns undefined
. I know it's an async function so it returns a value before http.get
finishes. How can I create a function that returns true/false after "receiving" data from a REST API?
edit:
I want to use this function at Guard canActivate
function.
I dont know how can I construct IF statement with observable.
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
let x = this.someService.isOk();
if(...){//if isOk return true
return true;
}else{
this.router.navigate(['/login']);
}
}