Upv*_*ote 6 node.js promise q sails.js waterline
这就是我想做的
var response = [];
Model.find().then(function(results){
for(r in results){
MyService.getAnotherModel(results[r]).then(function(magic){
response.push(magic);
});
}
});
//when finished
res.send(response, 200);
Run Code Online (Sandbox Code Playgroud)
然而它只返回[],因为异步的东西还没有准备好.我正在使用使用Q promise的sails.js.任何想法如何在所有异步调用完成后返回响应?
https://github.com/balderdashy/waterline#query-methods(承诺方法)
由于水线使用Q,您可以使用该allSettled方法.
您可以在Q文档中找到更多详细信息.
Model.find().then(function(results) {
var promises = [];
for (r in results){
promises.push(MyService.getAnotherModel(results[r]));
}
// Wait until all promises resolve
Q.allSettled(promises).then(function(result) {
// Send the response
res.send(result, 200);
});
});
Run Code Online (Sandbox Code Playgroud)