Skip to content

Instantly share code, notes, and snippets.

@CatTail
Last active June 13, 2017 02:49
Show Gist options
  • Save CatTail/c81866ef15512d4e142e42e9f4ee0475 to your computer and use it in GitHub Desktop.
Save CatTail/c81866ef15512d4e142e42e9f4ee0475 to your computer and use it in GitHub Desktop.
async/await wrapper for express middleware
// wrap async function to use as express middleware or route
// let wrap = fn => (...args) => fn(...args).catch(args[2])
function wrapRoute(fn) {
return function () {
return fn.apply(undefined, arguments)
.catch(arguments[arguments.length - 1]);
};
}
function wrapMiddleware(fn) {
return function () {
const next = arguments[arguments.length - 1];
return fn.apply(undefined, arguments)
.then(next.bind(null, null))
.catch(next);
};
}
@CatTail
Copy link
Author

CatTail commented Jun 13, 2017

@ekoome ES6 version will work of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment