title: Cancel JavaScript Promise
body: <p>I am trying to cancel a promise as given below:</p>
  
  <pre><code>function example(cancel = Promise.reject()) {
      return new Promise((resolve, reject) =&gt; {
          const timer = setTimeout(() =&gt; resolve('jack-jack'), 5000);
          cancel.then((res) =&gt; {
              clearTimeout(timer);
              reject('cancelled'); 
          }, ()=&gt;{})
      });
  }
  var cancel=Promise.reject(); 
  example(cancel).then((res) =&gt; console.log('res handled:' + res)).catch((err) =&gt; console.log('err handled:' + err));
  console.log('attempting cancellation of promise');
  cancel=Promise.resolve();
  </code></pre>
  
  <p>However I am not able to cancel it. What am I doing wrong over here?</p>