Last active
June 13, 2017 02:49
-
-
Save CatTail/c81866ef15512d4e142e42e9f4ee0475 to your computer and use it in GitHub Desktop.
async/await wrapper for express middleware
This file contains hidden or 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
// 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); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ekoome ES6 version will work of course.