Is it possible to create a function that can retrieve a token from a server, considering that the http.post()
method generates a response after the function has already returned the token?
How can I ensure that my function waits for the http.post()
call to complete before returning the token?
This is the code snippet in question:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class ServerConnectionService{
constructor( private http : Http) { }
token : string;
Login(Password : string, Username : string, ServerRootURL : string) : string
{
let url = ServerRootURL + "api/AdminApp/RegisterToken";
this.http.post(url, { "Username": Username, "Password": Password }).toPromise()
.then(res => this.token = res.json())
.catch(msg => console.log('Error: ' + msg.status + ' ' + msg.statusText))
return this.token;
}
}
Your help and suggestions are greatly appreciated.