desi Joe
desi Joe

Reputation: 373

how to return a nested promise

I have a JavaScript function where I am making some rest calls and using the responses to build the payload for the next call. Here is some sudo code that shows what I am trying to do. All my code is working fine; however, I am not sure how to return the promise p3 to the updateList caller. I really appreciate any help, thanks.

function updateList(listOfUsers){
    var p1 = getUser(userId1); // returns a promise    
    var p2 = getUser(userId2); // returns a promise
    $q.all([p1, p2]).then(function success(){
      ...some code to get the uses and build payload for next call...
      var p3 = updateList(payload); //also returns a promise 
      //how do I return p3?
    });
}

-dj

Upvotes: 1

Views: 56

Answers (2)

Sasank Sunkavalli
Sasank Sunkavalli

Reputation: 3964

So this is how nested promises work , u have return in resolve function also

return $q.all([p1, p2]).then(function success(){
      ...some code to get the uses and build payload for next call...
      return updateList(payload); //also returns a promise 
    });

Upvotes: 0

Nick Cox
Nick Cox

Reputation: 6452

function updateList(listOfUsers){
    var p1 = getUser(userId1); // returns a promise    
    var p2 = getUser(userId2); // returns a promise
    return $q.all([p1, p2]).then(function success(){
      ...some code to get the uses and build payload for next call...
      return updateList(payload); //also returns a promise 
    });
}

Upvotes: 1

Related Questions