上周我开始使用 Node.js 进行编码。我编写了代码来生成 JSON 查询多个表和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);
}
请让我知道是否有更好的方法来编写多个查询和构建 JSON。