SailsJS +水线并发数据库请求与承诺

我对SailsJS的吃水线并发性有些困惑。 目前我正在做这样的数据检索;

var results = {}; // Get user by id 5 User.find('5', function(err, user) { results.user = user; // when it resolves, get messages Message.find({userId: '5'}, function(err, messages) { results.messages = messages; // when message query resolves, get other stuff OtherStuff.find({userId: '5'}, function(err, otherStuff) { results.otherStuff = otherStuff; res.view({results}); }); }); }); 

问题是数据库调用不是并发的。 每一个请求都在先前的承诺完成之后启动。 我想同时启动所有请求,然后以某种方式查看是否所有的承诺都已完成,如果有,请继续将结果传递给视图。

我将如何实现与db请求的并发性?

谢谢!

使用async.autoasync模块在Sails中全球化:

 async.auto({ user: function(cb) { // Note--use findOne here, not find! "find" doesn't accept // an ID argument, only an object. User.findOne('5').exec(cb); }, messages: function(cb) { Message.find({userId: '5'}).exec(cb); }, otherStuff: function(cb) { OtherStuff.find({userId: '5'}).exec(cb); } }, // This will be called when all queries are complete, or immediately // if any of them returns an error function allDone (err, results) { // If any of the queries returns an error, // it'll populate the "err" var if (err) {return res.serverError(err);} // Otherwise "results" will be an object whose keys are // "user", "messages" and "otherStuff", and whose values // are the results of those queries res.view(results); } );