Utility function to use async functions as express handlers
yarn add @xpbytes/express-async-handler
import { asyncHandler } from '@xpbytes/express-async-handler'
app.get(
'/test',
asyncHandler(async (req, res, next) => {
const code = await Promise.resolve(204)
res.sendStatus(code).end()
})
)
You can optionally give a second argument errorHandler
:
function onError(err, req, res, next) {
// ...
}
app.get(
'/test',
asyncHandler(async (req, res, next) => {
const code = await Promise.resolve(204)
res.sendStatus(code).end()
}, onError)
)