Handling errors in express async middleware
I have an async middleware in express, because I want to use await inside it, to clean up my code. const express = require('express'); const app = express(); app.use(async(req, res, next) => { await authenticate(req); next(); }); app.get('/route', async(req, res) => { const result = await request('http://example.com'); res.end(result); }); app.use((err, req, res, next) => { console.error(err); res .status(500) .end('error'); }) app.listen(8080); The problem is that when it rejects, it doesn't go to my error middleware, but if I remove the async keyword and throw inside a middleware it does.