title: Async initialization of Angular 2 service
body: <p>I have an Angular 2 service that needs to do async work when it is initalized, and should not be usable before this initialization has been completed.</p>
  
  <pre class="lang-js prettyprint-override"><code>@Injectable()
  export class Api {
      private user;
      private storage;
  
      constructor(private http: Http) {
          this.storage = LocalStorage;
          this.storage.get('user').then(json =&gt; {
              if (json !== "") {
                  this.user = JSON.parse(json);
              }
          });        
      }
  
      // one of many methods
      public getSomethingFromServer() {
          // make a http request that depends on this.user
      }
  }
  </code></pre>
  
  <p>As it currently stands, this service is initialized, and returned immediately to any component that uses it. That component then calls <code>getSomethingFromServer()</code> in its <code>ngOnInit</code>, but at that point Api.user is not initialized, and therefore the wrong request is sent.</p>
  
  <p>The lifecycle hooks (<code>OnInit</code>, <code>OnActivate</code>, etc) does not work with services, only components and directives, so I cannot use those.</p>
  
  <p>Storing the Promise from <code>get()</code> call would require all the different methods that depend on user to wait for it, causing a lot of code duplication.</p>
  
  <p>What is the recommended way to do async initialization of services in Angular 2?</p>