$ q.all - 访问个人承诺结果?

时间:2017-09-19 19:07:59

标签: angularjs promise angular-promise q

我对$ q角度承诺有点新鲜。这是我的代码结构:

$q.all(promises).then(function(results) {
    results.forEach(function(data, status, headers, config) {
        console.log(status);
    });
    $scope.saveDocumentCaseState();
}, function errorCallBack(response) {
    console.log('error while mass update case entries');
    console.log(response);
    $scope.submitToWsSuccessful = 2;
});

是否有可能在下一个承诺

之前执行 时执行每个承诺的结果?

1 个答案:

答案 0 :(得分:1)

假设您从名为promiseFactory的角度工厂收到一个具有任意数量承诺的数组:

var promises = promiseFactory.getPromises(); // Returns an array of promises

promises.forEach(function(p){
  p.then(promiseSuccess, promiseError);

  function promiseSuccess(){
    // Do something when promise succeeds
  }

  function promiseError(){
    // Do something when promise errors
  }
});

$q.all(promises).then(allSuccess, allError);

function allSuccess(){
  // All calls executed successfully
}

function allError(){
  // At least one of the calls failed
}