Skip to content

Instantly share code, notes, and snippets.

@acanimal
Created December 6, 2017 16:05
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 acanimal/3d5992cd1000383b669f5092b27e0dbb to your computer and use it in GitHub Desktop.
Save acanimal/3d5992cd1000383b669f5092b27e0dbb to your computer and use it in GitHub Desktop.
Helper to use async middlewares
const asyncHandler = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
// Usage
express.get('/', asyncHandler(async (req, res, next) => {
const bar = await foo.findAll();
res.send(bar)
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment