Created
December 6, 2017 16:05
-
-
Save acanimal/3d5992cd1000383b669f5092b27e0dbb to your computer and use it in GitHub Desktop.
Helper to use async middlewares
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 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