I have started coding in Node.js last week. I have wrote code to generate JSON querying multiple tables and not sure if this is correct way of coding asynchronously
build_actor(post, function(actor){
build_post_object(post, function(post_object){
build_post_attachments(post, function(attachments){
build_post_comments(post, function(comments){
var post_obj = {};
post_obj.actor = actor;
post_obj.object = post_object;
post_obj.attachments = attachments;
post_obj.comments = comments;
console.log(JSON.stringify(post_obj)); // generates JSON
});
});
});
});
function build_actor(post, callback){
//query
callback(actor);
}
function build_post_object(post, callback){
//query
callback(post_object);
}
function build_post_attachments(post, callback){
//query
callback(attachments);
}
function build_post_comments(post, callback){
//query
callback(comments);
}
Kindly let me know if there is a better way for writing multiple queries and building a JSON.