【发布时间】:2015-03-31 04:36:35
【问题描述】:
我有以下(向数组添加承诺):
function processInformation() {
var promisesArray = [];
for(var i =0; i<a.length; i++) {
var promise = this.getValue().then((response) => {
this.processValue(response).then(()=> {
// it gets here
});
});
promises.push(promise).
}
return $q.all(promises);
}
然后我这样打电话:
getInformation().then((response) =>{
// wait for the promises to be resolved
this.processInformation().then((r) => {
// never gets here
});
}).finally(()=> {
//gets here
});
这没有按预期工作,因为它确实调用了 finally,并且它永远不会进入 processInformation。我还尝试使用 angular.forEach 而不是 for 循环(如其他帖子中所述),但它仍然无法正常工作。它确实进入了 processValue() 的 then 内部。还有什么方法可以解决这个问题?
【问题讨论】:
-
您的帖子自相矛盾。你说它永远不会进入
processInformation,它确实进入processValue,但是你发布的第一个函数是processInformation和processValue是inside那。你能发布一个不自相矛盾的实际例子吗?此外,我们不知道this、a或其他各种东西是什么。这里有很多缺失的信息。最后,看起来您在(假设)调用processInformation时可能使用了错误的this。有没有您没有告诉我们的控制台错误?