Skip to content

Instantly share code, notes, and snippets.

@ChrisBarberRiley
Created November 7, 2019 20:50
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 ChrisBarberRiley/a4eced2e75033c26bc6d975677762796 to your computer and use it in GitHub Desktop.
Save ChrisBarberRiley/a4eced2e75033c26bc6d975677762796 to your computer and use it in GitHub Desktop.
const asyncHandler = fn => (req, res, next) =>
Promise
.resolve(fn(req, res, next))
.catch(next);
module.exports = asyncHandler;
// Implementation (Controller method)
exports.someRoute = asyncHandler(async (req, res, next) => {
const foo = await Bar.find();
res.status(200).json({bar});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment