Last active
May 4, 2020 02:22
-
-
Save fredyang/f6d4526227c24baea5169876fdc5fb80 to your computer and use it in GitHub Desktop.
a function that allow you convert async express middleware without worry about exception
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
const catchit = asynFn => (req, res, next) => asynFn(req, res, next).catch(next); | |
//usage | |
router.get('/', catchit(async (req, res, next) => { | |
await data = db.get(); | |
throw 'err'; | |
res.json(data); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment