Skip to content

Instantly share code, notes, and snippets.

@jaxbot
Last active August 29, 2015 14:05
Show Gist options
  • Save jaxbot/2a2072a77372314485ff to your computer and use it in GitHub Desktop.
Save jaxbot/2a2072a77372314485ff to your computer and use it in GitHub Desktop.
Node.js generators vs. callbacks
function home() {
db.query("SELECT * FROM `posts` ORDER BY `time` DESC LIMIT 10", function(err, rows){
render('home', { posts: rows[0] }, function(err, result){
response.end(result);
});
});
}
function post(id) {
db.query("SELECT * FROM `posts` WHERE `link` = " + db.escape(id)), function(err, rows){
render('post', { post: rows[0][0] }, function(err, result){
response.end(result);
});
});
}
function *home() {
var results = yield db.query("SELECT * FROM `posts` ORDER BY `time` DESC LIMIT 10");
this.body = yield render('home', { posts: results[0] });
}
function *post(id) {
var results = yield db.query("SELECT * FROM `posts` WHERE `link` = " + db.escape(id));
this.body = yield render('post', { post: results[0][0] });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment