Created
December 5, 2017 14:03
-
-
Save guyb7/a0c9fe1b937650b424ee495af21910f0 to your computer and use it in GitHub Desktop.
A wrapper around Express middlewares to support promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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