您应该查看 async.js 库。对于您的问题,请使用 async.parallel:
npm install --save async
var async = require("async");
async.parallel({
course: function (callback) {
// course query here
callback(null, results_from_coursequery); // null is error - you can also assign the error variable from query.
},
section: function (callback) {
// course query here
callback(null, results_from_sectionquery); // null is error - you can also assign the error variable from query.
}
},
function(err,results) {
if(err) {res.json(err);}
else {res.send(results);} // results is now an object similar to: {course: [object], section: [object]}
});