Created
July 21, 2013 10:08
-
-
Save luin/6048140 to your computer and use it in GitHub Desktop.
Make express res.json support promise
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
// 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