I was wondering what's the best way to handle nested mysql-queries is nodejs.
So something like this:
connection.query("SELECT * FROM blogs ORDER BY time DESC", function(err, blogs, fields) {
for (blog in blogs) {
connection.query("SELECT * FROM tags WHERE blog_id='blog.id' ", function(err, tags, fields) {
blog.tags = tags
});
}
res.send(blogs)
});
This obviously doesn't work, because of the async nature. The result already gets returned before the tags are fetched.
I've been reading up on node and callbacks and promises seems to be the way to go. But I'm unable to see how I would best use them in this small example.
Thx!
Q.spreadmethod (or maybe these reduction examples