Skip to content

Instantly share code, notes, and snippets.

@bahmutov
Last active August 7, 2017 17:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahmutov/8c8da5db0f33acceb057 to your computer and use it in GitHub Desktop.
Save bahmutov/8c8da5db0f33acceb057 to your computer and use it in GitHub Desktop.
Remove boilerplate when connecting promise-returning middleware to Express
// 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();
});
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();
});
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