Express does not support async middlware. This PR uses express-async-handler to wrap the the async middleware in the example in the README to avoid confusion.
If you use unwrapped async middleware in express and an error occurs in the middleware, the request will hang and an UnhandledPromiseRejectionWarning will fire.
app.get('/async-error', async (req, res) => {
throw new Error(`I'm going to wreak havok`)
})
However, if you wrap the async middleware with express-async-handler, errors are handled as expected.
const asyncHandler = require('express-async-handler')
app.get('/wrapped-async-error', asyncHandler(async (req, res) => {
throw new Error(`I'll be handled just like an error in sync context`)
}))
Express does not support async middlware. This PR uses
express-async-handler
to wrap the the async middleware in the example in the README to avoid confusion.If you use unwrapped async middleware in express and an error occurs in the middleware, the request will hang and an
UnhandledPromiseRejectionWarning
will fire.However, if you wrap the async middleware with
express-async-handler
, errors are handled as expected.Note that
koa
supports async middleware natively.