title: How to wait for Http answer in TypeScript - Angular 5
body: <p>i want to make a function that returns a token from a server but <code>http.post()</code> gets a response after my function returned the token.</p>
  
  <p>How do i wait for <code>http.post()</code> before returning the token.</p>
  
  <p>My code:</p>
  
  <pre><code>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 =&gt; this.token =  res.json())
              .catch(msg =&gt; console.log('Error: ' + msg.status + ' ' + msg.statusText))
          return this.token;
      }
  }
  </code></pre>
  
  <p>Thank you in advance.</p>