Skip to content

Instantly share code, notes, and snippets.

@saml
Created June 5, 2020 17:34
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 saml/f60aba55896c26de02b646c78f9b4d5a to your computer and use it in GitHub Desktop.
Save saml/f60aba55896c26de02b646c78f9b4d5a to your computer and use it in GitHub Desktop.
// 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