Skip to content

Instantly share code, notes, and snippets.

@luin
Created July 21, 2013 10:08
Show Gist options
  • Save luin/6048140 to your computer and use it in GitHub Desktop.
Save luin/6048140 to your computer and use it in GitHub Desktop.
Make express res.json support promise
// Let express support "Promise"
app.use(function(req, res, next) {
var oldResJson = res.json;
res.json = function(promise) {
if (promise &&
typeof promise === 'object' &&
typeof promise.success === 'function') {
promise.success(function(fetchedObj) {
if (fetchedObj) {
oldResJson.call(res, fetchedObj);
} else {
next(404);
}
});
} else {
oldResJson.apply(res, arguments);
}
};
next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment