title: NodeJS Timeout a Promise if failed to complete in time
body: <p>How can I timeout a promise after certain amount of time?
  I know Q has a promise timeout, but I'm using native NodeJS promises and they don't have .timeout function.</p>
  
  <p>Am I missing one or its wrapped differently?</p>
  
  <p>Alternatively, Is the below implementation good in means of not sucking up memory, actually working as expected?</p>
  
  <p>Also can I make it somehow wrapped globally so I can use it for every promise I create, without having to repeat the setTimeout and clearTimeout code?</p>
  
  <pre><code>function run() {
      logger.info('DoNothingController working on process id {0}...'.format(process.pid));
  
      myPromise(4000)
          .then(function() {
              logger.info('Successful!');
          })
          .catch(function(error) {
              logger.error('Failed! ' + error);
          });
  }
  
  function myPromise(ms) {
      return new Promise(function(resolve, reject) {
          var hasValueReturned;
          var promiseTimeout = setTimeout(function() {
              if (!hasValueReturned) {
                  reject('Promise timed out after ' + ms + ' ms');
              }
          }, ms);
  
          // Do something, for example for testing purposes
          setTimeout(function() {
              resolve();
              clearTimeout(promiseTimeout);
          }, ms - 2000);
      });
  }
  </code></pre>
  
  <p>Thanks! </p>