Created
November 12, 2017 17:38
-
-
Save martypdx/e36eab1aedf4dbf75a72c9d2a9eec568 to your computer and use it in GitHub Desktop.
A helper function for promise returning express middleware
This file contains 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
// Assumes: | |
// 1. application/json response content type | |
// 2. registered error handler capable of handling rejections | |
module.exports = function respond(handler) { | |
return async (req, res, next) => { | |
try { | |
const result = await handler(req); | |
res.json(result); | |
} | |
catch(err) { | |
next(err); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use in express routes like:
Can be used with simple Promise returning middleware without async: