Skip to content

Instantly share code, notes, and snippets.

@Arsfiqball
Created August 2, 2019 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arsfiqball/17d47bba6bc269b25df883ec3933a334 to your computer and use it in GitHub Desktop.
Save Arsfiqball/17d47bba6bc269b25df883ec3933a334 to your computer and use it in GitHub Desktop.
Wrap async function to express compatible middleware
// wrapper function
function wrap (fn) {
return function (req, res, next) {
return fn(req, res, next).catch(err => {
// handle general error here
next(err)
})
}
}
// usage as express middleware
app.use(wrap(async function (req, res, next) {
const users = await req.db('users')
res.status(200).json({ users })
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment