Last active
August 29, 2015 14:05
-
-
Save jaxbot/2a2072a77372314485ff to your computer and use it in GitHub Desktop.
Node.js generators vs. callbacks
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
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); | |
}); | |
}); | |
} |
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
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