title: How can I clear timeout for an asynchronous function in JavaScript
body: <p>Let me be very clear so as to avoid confusion.
  I have a sleep function (below) which timeouts for as many millisecond as I specify.</p>
  
  <pre><code>function sleep(ms) 
  {
      return new Promise(resolve =&gt; setTimeout(resolve, ms));
  }
  </code></pre>
  
  <p>Now in another function beginTest() I called sleep for some millisecond let say 5000 ms. </p>
  
  <pre><code>async function beginTest()
  {
      await sleep(5000);
  }
  </code></pre>
  
  <p>I have a separate function which gets executed when a button is clicked. I want the function to clear the timeout whenever the button is clicked before completion of 5000 ms. I understand that after 5000 ms clicking the button should not affect anything as the promise has already been resolved.</p>
  
  <pre><code>document.getElementById("reactTimeClick").onmousedown = function()
  {
      cleartimeOut(); // Clear timeout of above function
  }
  </code></pre>