title: How to handle an unhandled promise rejection asynchronously?
body: <p>I'm trying to wrap my head around this issue I'm facing concerning async/await and Promises. I managed to boil my issue down to the following code:</p>
  
  <pre><code>async function sleep(ms: number) {
    return new Promise(resolve =&gt; {
      setTimeout(resolve, ms);
    });
  }
  
  async function fetchMock(): Promise&lt;any&gt; {
    return new Promise(() =&gt; {
      throw 'error fetching result';
    });
  }
  
  async function main(): Promise&lt;any&gt; {
    const kickedOffRequest = fetchMock();
    await sleep(10);
    return kickedOffRequest;
  }
  
  main()
    .then(() =&gt; console.log('resolved promise!'))
    .catch(error =&gt; console.error('caught error!', error));
  </code></pre>
  
  <p>I receive the following warning:</p>
  
  <pre><code>(node:82245) UnhandledPromiseRejectionWarning: error fetching result
  (node:82245) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  (node:82245) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  caught error! error fetching result
  (node:82245) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1)
  </code></pre>
  
  <p>You can observe the same issue <a href="https://repl.it/repls/PositiveBrokenPrinters" rel="noreferrer">in this sandbox</a>. I noticed that commenting out the <code>await sleep(10)</code> fixes the issue, but I apparently know less about promises than I thought. Why does commenting that line out make my program work? I'm tempted to ask how to fix the <code>Promise rejection was handled asynchronously</code> error, but I hope that once I understand how <code>await sleep(10)</code> causes the error I get I will be able to fix this one on my own. </p>
  
  <p>Thanks in advance for taking the time to read/answer this question!</p>