【问题标题】:Angularjs q.all firing before promises concludeAngularjs q.all 在承诺结束之前触发
【发布时间】: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,但是你发布的第一个函数processInformationprocessValueinside那。你能发布一个不自相矛盾的实际例子吗?此外,我们不知道thisa 或其他各种东西是什么。这里有很多缺失的信息。最后,看起来您在(假设)调用processInformation 时可能使用了错误的this。有没有您没有告诉我们的控制台错误?

标签: angularjs angular-promise


【解决方案1】:

你创建的promise对象没有返回promise对象,你也应该从内部promise返回响应来解决它。

代码

function processInformation() {
    var promisesArray = [];
    for(var i =0; i<a.length; i++) {
      var promise = this.getValue().then((response) => {
            return this.processValue(response).then((data)=> {
                  // it gets here
                  return data;
            });
       });
      promises.push(promise).
    }

 return $q.all(promises);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2016-08-13
    • 2018-02-28
    相关资源
    最近更新 更多