【发布时间】:2014-03-23 15:36:17
【问题描述】:
我有一个返回随机学生的异步函数。现在我想要一个返回两个独特学生的函数——我的问题的根源。
getTwoRandom = function(req) {
var deferred = Q.defer();
Q.all([
Student.getRandom(req),
Student.getRandom(req)
])
.then(function(students){
if(students[0]._id !== students[1]._id) { //check unique
deferred.resolve(students);
} else {
//students are the same so try again... this breaks
return getTwoRandom(req);
}
});
return deferred.promise;
};
然后再往下我有这样的东西:
getTwoRandom(req).then(function(students) {
//do what I want...
});
问题是当我执行return getTwoRandom(req); 时,.then() 函数不会触发...这是否返回了 .then() 未使用的不同承诺?
【问题讨论】:
标签: javascript node.js asynchronous q