title: Angular 2 equivalent for $timeout
body: <p>I have to use (large amounts of) existing code in an Angular 2 environment. That code makes extensive use of the <a href="https://docs.angularjs.org/api/ng/service/$timeout" rel="noreferrer"><code>$timeout</code> service</a> from AngularJS 1.x. As opposed to various other AngularJS 1.x services that are used in the code, I am having a hard time finding a information about an Angular 2 equivalent for the <code>$timeout</code> service.</p>
  
  <p>The <a href="https://angular.io/api" rel="noreferrer">Angular docs</a> do not seem to contain any mention of a service with <code>timeout</code>-something in its name. The article <a href="https://angular.io/guide/upgrade" rel="noreferrer"><em>Upgrading from AngularJS</em></a> does mention the scenario I am facing:</p>
  
  <blockquote>
    <p>Maybe you want access to AngularJS's built-in services like <code>$location</code> or <code>$timeout</code>.</p>
  </blockquote>
  
  <p>Unfortunately, the article does not actually explain how to access those particular services, as the subsequent example <code>HeroesService</code> assumes a service without any dependencies supplied by AngularJS 1.x.</p>
  
  <p>Articles such as <a href="http://www.codelord.net/2015/10/14/angular-nitpicking-differences-between-timeout-and-settimeout/" rel="noreferrer">this one</a> suggest using the native <a href="http://mdn.beonex.com/en/DOM/window.setTimeout.html" rel="noreferrer"><code>setTimeout</code> function</a> does not live up to the capabilities of the <code>$timeout</code> services, either.</p>
  
  <p><strong>How can I reproduce the <code>$timeout</code> functionality in the Angular 2 environment?</strong></p>
  
  <p>EDIT: As has been noted in the answers, the drawbacks of the native <code>setTimeout</code> function are not relevant when using Angular 2. In that case, if I had the full <code>$q</code> from AngularJS 1.x, I could replicate the <code>$timeout</code> function roughly like this:</p>
  
  <pre><code>function $timeout(fn, delay) {
      var result = $q.defer();
      setTimeout(function () {
          $q.when(fn()).then(function (v) {
              result.resolve(v);
          });
      }, delay);
      return result.promise;
  }
  </code></pre>