I have problem of returning a value from a method, the value is always undefined and the promise will only yield the value after the method has returned.
I have tried observable but not getting the desired results.
import { Router } from "@angular/router";
import { JwtHelperService } from "@auth0/angular-jwt";
import { HttpClient } from "@angular/common/http";
import { map } from "rxjs/operators";
import { BehaviorSubject, Observable, of, Subject } from "rxjs";
interface ServerStatus {
status: string;
}
interface BaseApi {
baseURL: string;
apiURL: string;
}
const helper = new JwtHelperService();
@Injectable({
providedIn: "root"
})
export class AuthService {
public token: string = "";
public api: string;
constructor(
private httpService: HttpClient,
private router: Router
) {
}
public decodedToken: JSON;
public role: string;
public timersubscribe: any;
public myDate: Date;
public stage: number;
public tmp: any;
getStage() {
let url ;
this.getApi().then(ret => url = ret))
//this will appear only after the return is done
console.log(url);
//the url is always undefined
return this.httpService.get<ServerStatus>(url);
}
getApi() {
return this.httpService.get<BaseApi>("../../assets/api.json").toPromise().then(result => {
return result.apiURL;
})
}
}
I want the promise to resolved to string before the method ( getStage() ) return.