title: Angular 2: Http asynchronous call
body: <p>I am using Http Service from Angular2 :</p>
  
  <p><strong>My login component:</strong></p>
  
  <pre><code>    doLogin() {
          this.authenticationService.inValidateSession();    
   this.authenticationService.userConfiguration=this.httpService.login(this.user.email,this.user.password)
          this.router.navigate(['/site']);
  
      }
  </code></pre>
  
  <ul>
  <li>Once user submits credentials , dologin with invalidate the old session if any.</li>
  <li>Will do the server login</li>
  <li>And will save the user configuration it will get from the server like name and groups...
  _ And will redirect to default homepage.</li>
  </ul>
  
  <p><strong>HttpService</strong></p>
  
  <pre><code>login(username:string,password:string) {
       let headers = new Headers();
       this.createAuthorizationHeader(headers,username,password);
       this.http.get(this.url,{headers:headers}).subscribe(this.extractData,this.handleError);
       return this.configFromLogin;  
  }
  
  private extractData(res: Response) {
    if(res.status==200){
      let body = res.json();
      var result={"config":body,
                  "x-xsrf-token":res.headers.get('x-xsrf-token')};
      this.configFromLogin=result;
    }
    else{
      //console.log(error);
      // throw exception or use this.error() method
    }
  }
  </code></pre>
  
  <p>Since <strong>Server provides Token</strong> authentication which we need to use for all the Post Requests, i save it in Session.</p>
  
  <p><strong>My Problems or Doubts:</strong></p>
  
  <ol>
  <li>When i debug <strong>doLogin()</strong> codes get <strong>executed first</strong>
  (saveInSession) even <strong>before HttpService extractData</strong> is executed.
  Why ? i know Http get is async call but how can i make doLogin wait
  ?</li>
  <li>In HttpService i am checking if response is 200 then i am saving it
  otherwise i want to throw error and use the HandleError method ,
  which i am using in subscribe method of Observable. Is it possible ?</li>
  </ol>
  
  <p>Please help me with above two problems, in case of questions i am available to answer.</p>
  
  <p>Thanks</p>