Created
June 5, 2020 17:34
-
-
Save saml/f60aba55896c26de02b646c78f9b4d5a to your computer and use it in GitHub Desktop.
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
// Utilities for express. | |
/** | |
* Wraps request handler and calls next() on error. | |
* @param {Function} reqHandler express request handler. It doesn't have to return Promise. | |
*/ | |
function asyncWrap(reqHandler) { | |
async function handlerWrap(req, res, next) { | |
try { | |
// reqHandler might not return Promise. So, can't do reqHandler().catch(); | |
return await reqHandler(req, res, next); | |
} catch (err) { | |
next(err); | |
} | |
} | |
return handlerWrap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment