Skip to content

Instantly share code, notes, and snippets.

Created February 13, 2013 11:00
Show Gist options
  • Save anonymous/4943862 to your computer and use it in GitHub Desktop.
Save anonymous/4943862 to your computer and use it in GitHub Desktop.
Callback pattern for APIs
// inspiration: https://github.com/damienklinnert/appdotnet
var request = require('request');
foo = function(cb) {
var opts = { uri: 'http://appd01:8111' };
request.get(opts, function(err ,resp, body) {
if (err) { return cb(err); }
return cb(null, resp, body);
});
};
foo(function(err, response, body) {
if (err) {
console.log("error: %s", err);
} else {
console.log("status code: %s", response.statusCode);
console.log("body:\n %s", body);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment