title: Handling errors in express async middleware
body: <p>I have an <code>async</code> middleware in express, because I want to use <code>await</code> inside it, to clean up my code.</p>
  
  <pre><code>const express = require('express');
  const app = express();
  
  app.use(async(req, res, next) =&gt; {
      await authenticate(req);
      next();
  });
  
  app.get('/route', async(req, res) =&gt; {
      const result = await request('http://example.com');
      res.end(result);
  });
  
  app.use((err, req, res, next) =&gt; {
  
      console.error(err);
  
      res
          .status(500)
          .end('error');
  })
  
  app.listen(8080);
  </code></pre>
  
  <p>The problem is that when it rejects, it doesn't go to my error middleware, but if I remove the <code>async</code> keyword and <code>throw</code> inside a middleware it does.</p>
  
  <pre><code>app.get('/route', (req, res, next) =&gt; {
      throw new Error('Error');
      res.end(result);
  });
  </code></pre>
  
  <p>So I'm getting <code>UnhandledPromiseRejectionWarning</code> instead of entering my error handling middleware, how can I let the error bubble up, and express handle it?</p>