title: What is the best general practice to timeout a function in promise
body: <p><strong>Promisify a function call with timeouts</strong></p>
  
  <p>I have seen many resources provide similar examples of using <code>Promise.race</code> to timeout a function call within a given period of time. This is a very good example of how <code>Promise.race</code> can be used in practice. Here's some sample code:</p>
  
  <pre><code>function doWithinInterval(func, timeout) {
      var promiseTimeout = new Promise(function (fulfill, reject) {
         // Rejects as soon as the timeout kicks in
         setTimeout(reject, timeout);
      });
      var promiseFunc = new Promise(function (fulfill, reject) {
          var result = func(); // Function that may take long to finish
          // Fulfills when the given function finishes
          fulfill(result);
      });
  
      return Promise.race([promiseTimeout, promiseFunc]);
  }
  </code></pre>
  
  <p>The simple approach above using <code>Promise.race</code> rejects the promise as soon as the timeout kicks in before <code>func</code> has completed. Otherwise, the project is fulfilled once the <code>func</code> function finishes before the timeout interval.</p>
  
  <p>This sounds good and easy to use.</p>
  
  <p><strong>However, is this the best practice to use timeout in Promise?</strong></p>
  
  <p>Surely, the approach above can be employed if we want to set a timeout against a function call using Promises. The operations still appear to be a good promise. However, is this considered a good practice of using timeout in a Promise? If not, what is the disadvantage of using this? </p>
  
  <p><strong><em>I've look for alternative approaches, but couldn't find a native Promise way to do this.</em></strong></p>
  
  <p>Instead, some external Promise libraries offer <code>timeout</code> functionality as follows:</p>
  
  <ul>
  <li><p><strong>Bluebird</strong> supplies <a href="http://bluebirdjs.com/docs/api/timeout.html" rel="noreferrer"><code>.timeout()</code></a></p></li>
  <li><p><strong>WinJS</strong> supplies <a href="https://msdn.microsoft.com/en-us/library/windows/apps/br229729.aspx" rel="noreferrer"><code>.timeout()</code></a> as well</p></li>
  <li><p><strong><a href="https://github.com/kriskowal/q" rel="noreferrer">Q</a></strong> also comes with <code>.timeout()</code>.</p></li>
  </ul>
  
  <p>However, <code>Promise.timeout()</code> doesn't appear to be part of the standard ECMAScript 6 API (please correct me if I'm wrong). Is there any recommended way to handle timeouts natively with ES6 Promises?</p>