Deniss M.
Deniss M.

Reputation: 4050

$q.all - access individual promise result?

I'm a bit new to $q angular promises. Here is my code structure:

$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;
});

Is there a possibility to access the result of each promise when it has been executed before the next one?

Upvotes: 0

Views: 325

Answers (1)

Matthew Cawley
Matthew Cawley

Reputation: 2818

So assuming that you receive an array with an arbitrary number of promises from an angular factory called 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
}

Upvotes: 1

Related Questions