Skip to content

Conversation

jordanbtucker
Copy link

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`) }))

Note that koa supports async middleware natively.

Express does not support async middlware. Use express-async-handler to wrap the the async middleware in the example in the README.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants