3

I have a little question about multiple promise. How can I wait that all promises will be done for return the final result.

See my code :

getInfo : function(){
        return promiseA.then(function(result){
               info  = result;
               //this function have also promises 
               return ServiceA.functionA(info.login)
                      .then(function(favouriteItems){
                          info.favorites = favouriteItems;
                          return $q.when(info);
                       });      
         });       
},

My aims it's to wait the result of ServiceA.functionA before return value.

Thanks

K.L

3
  • 5
    Use $q.all(). Commented Apr 28, 2014 at 14:15
  • In this situation impossible to use $q.all(), because function ServiceA.functionA need to use result of promiseA. Commented Apr 29, 2014 at 7:08
  • It looks to me like you already are. The promise returned by getInfo will be resolved with info after its favorites item has been modified in the final callback. Commented May 3, 2014 at 16:40

4 Answers 4

1

I wrote an answer to another question stating the solution to this problem using the $q.all approach.

Check it out: AngularJS: Listen to events, one after the other

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use $q.all()

This is a good post here on the question: stackoverflow.com/questions/21310964/angularjs-q-all

Comments

0
function getInfo() {
  var deferred = $q.defer();

  promiseA.then(function(result){
    info  = result;
    // this function have also promises 
    ServiceA.functionA(info.login)
      .then(function(favouriteItems){
         info.favorites = favouriteItems;
           // the magic
           deferred.resolve(info);
           // at this point, you can resolve any value
      });      
    });
  }

  return deferred.promise;
}

then later you can call that function and get a promise...

var promise = getInfo();
promise.then(successCallback, errorCallback, notifyCallback);

The successCallback will only be called once resolve has been called on the deferred object.

1 Comment

you manually call it with $q.reject(reason) in the error callbacks of the other requests.
0
getInfo : function() {
  return promiseA.then(function(result){           
    return ServiceA.functionA(result.login).then(function(favouriteItems){
      result.favorites = favouriteItems;
      return result;
    });      
  });       
},

Use like this:

api.getInfo().then(function(result){
  // use result and result.favorites
}, function(err){
  // here you can be if an error occurred in promiseA or in ServiceA.functionA
})

1 Comment

It is always encouraged to give at least a little explanation what your code is doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.