title: Augment ES6 Promise with cancel method
body: <p>I'm trying to write some code which returns an ES6 promise after initiating some potentially long running asynchronous activity. However, I would like the possibility of being able to cancel that activity, so I want to augment my Promise with a 'cancel' method.</p>
  
  <p>An <a href="http://sscce.org/" rel="nofollow">sscce</a> which illustrates what I am trying to do is as follows:</p>
  
  <pre><code>function TimerPromise(timeInterval) {
      var timer;
  
      var p = new Promise(
          function(resolve,reject) {
              timer = setTimeout(
                  function() {
                      resolve(true);
                  },
                  timeInterval
              );
          }
      );
  
      p.cancel = function() {
          clearTimeout(timer);
      };
  
      console.log("p.cancel is ",p.cancel);
  
      return p;
  }
  
  var t = TimerPromise(2000).then(function(res) { console.log("Result is ",res); });
  
  t.cancel();
  </code></pre>
  
  <p>In the example TimerPromise just sets a timer to simulate the long running asynchronous activity.</p>
  
  <p>This is what I get when running this:</p>
  
  <pre><code>$ node test.js
  p.cancel is  function () {
                  timer.clearTimeout();
          }
  /home/harmic/js/src/test.js:28
  t.cancel();
    ^
  
  TypeError: t.cancel is not a function
      at Object.&lt;anonymous&gt; (/home/harmic/js/src/test.js:28:3)
      at Module._compile (module.js:413:34)
      at Object.Module._extensions..js (module.js:422:10)
      at Module.load (module.js:357:32)
      at Function.Module._load (module.js:314:12)
      at Function.Module.runMain (module.js:447:10)
      at startup (node.js:141:18)
      at node.js:933:3
  </code></pre>
  
  <p>For some reason, the cancel method which I added to the promise has disappeared after leaving the function!</p>
  
  <p>Is there some reason why I cannot add properties or methods to an ES6 Promise? Or is this some peculiarity of the V8 implementation of Promises?</p>
  
  <p>For bonus points - I would like to be able to reject the promise if the cancel method is called, something like this:</p>
  
  <pre><code>p.cancel = function() {
      timer.clearTimeout();
      reject(new Error("Request Cancelled"));
  };
  </code></pre>
  
  <p>However, I cannot access the reject function outside the Promise executor, and inside the Promise executor I cannot access the promise itself (or can I?) so I cannot augment the Promise inside there.</p>
  
  <p>Is there any sensible pattern for doing this?</p>
  
  <p>Note: I am aware that <a href="http://bluebirdjs.com/docs/api/cancellation.html" rel="nofollow">Bluebird</a> provides Cancellable promises as an extension. I was hoping to implement this using ES6 native promises.</p>
  
  <p>I am using node.js 0.12, although I'd like this to work in current browsers also.</p>