Skip to content

Instantly share code, notes, and snippets.

@fredyang
Last active May 4, 2020 02:22
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 fredyang/f6d4526227c24baea5169876fdc5fb80 to your computer and use it in GitHub Desktop.
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
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