Last active
August 7, 2017 17:34
-
-
Save bahmutov/8c8da5db0f33acceb057 to your computer and use it in GitHub Desktop.
Remove boilerplate when connecting promise-returning middleware to Express
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
// use point-free callbacks | |
// http://glebbahmutov.com/blog/point-free-programming-is-not-pointless/ | |
var middleware = require('./middleware'); | |
app.get('example/uri', function (req, res, next) { | |
middleware.first(req, res) | |
.then(next) | |
.catch(res.json) | |
.done(); | |
}, function (req, res, next) { | |
middleware.second(req, res) | |
.then(next) | |
.catch(res.json) | |
.done(); | |
}); |
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
var middleware = require('./middleware'); | |
app.get('example/uri', function (req, res, next) { | |
middleware.first(req, res) | |
.then(function () { next(); }) | |
.catch(res.json) | |
.done(); | |
}, function (req, res, next) { | |
middleware.second(req, res) | |
.then(function () { next(); }) | |
.catch(res.json) | |
.done(); | |
}); |
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
var middleware = require('./middleware'); | |
// move wrap to separate file later | |
function wrap(middleware) { | |
return middlewareFn(req, res, next) { | |
middleware(req, res) | |
.then(next) | |
.catch(res.json) | |
.done(); | |
}; | |
} | |
app.get('example/uri', wrap(middleware.first), wrap(middleware.second)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment