Skip to content

Instantly share code, notes, and snippets.

@guyb7
Created December 5, 2017 14:03
Show Gist options
  • Select an option

  • Save guyb7/a0c9fe1b937650b424ee495af21910f0 to your computer and use it in GitHub Desktop.

Select an option

Save guyb7/a0c9fe1b937650b424ee495af21910f0 to your computer and use it in GitHub Desktop.
A wrapper around Express middlewares to support promises
const asyncMiddleware = promise => {
return (req, res) => {
promise(req)
.then(data => {
res.status(200).json({
success: true,
...data
})
})
.catch(e => {
console.error(e)
res.status(500).json({
success: false,
error: e.message
})
})
}
}
const getUser = async req => {
return {
user: req.user,
session: req.session
}
}
const app = express()
app.get ('/api/user', asyncMiddleware(getUser))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment