title: Angular Wait until variable has been initialized
body: <p>I'm about to create a service for a web application. The first step is to get a token via the rest API. As soon as this token is available, it should be sent as a header to get more information.</p>
  
  <p>My problem is that it executes the function to get more information before the variable is set. ( The header is empty, but if I set the token manually it works).</p>
  
  <p>How can I make my function wait until the variable has its value?</p>
  
  <p>Thanks</p>
  
  <pre><code>gettoken(): void {
  this.http.get&lt;InitResponse&gt;('URL' + '/token').subscribe(data =&gt; {
      this.token = data.token;
      localStorage.setItem('token', data.token);
      getmoreInfo();
    },
    (err: HttpErrorResponse) =&gt; {
      if (err.error instanceof Error) {
        console.log('Client-side error occured.');
      } else {
        console.log('Server-side error occured.');
      }
    }
  );
  </code></pre>
  
  <p>}</p>