zorlac
zorlac

Reputation: 75

AngularJS $q: How to chain promises then do parallel last in the chain?

I am trying to chain series of tasks, that returns a promise, the do parallel execution at the last of the chain. The code below doesn't work, I thought you can pass any object that returns a promise inside "then". Is there a proper way of implementing this. Thank you.

var startTask = $q.when (  );

startTasks
   .then ( validate )
   .then ( savePayment )
   .then ( refetchPayment )
   .then ( saveContactInfo )
   .then ( $q.all ( [ updateStatus, submitOrder ] ) )
   .then ( successHandler )
   .catch ( errorHandler );

Upvotes: 0

Views: 583

Answers (1)

Adam Jenkins
Adam Jenkins

Reputation: 55613

Aha, $q.all( [ updateStatus, submitOrder] ) always returns an immediately resolved promise.

What you want to do is probably something like this (guessing)

.then(function(response) {
    return $q.all( [ updateStatus(response), submitOrder(response) ] );
 });

See the difference?

[updateStatus,submitOrder] is just an array of function references, calling $q.all on an array of things that aren't promises will always return an immediately resolved promise.

You need to call those functions, as (I'm assuming) those functions return promises when called.

$q.all takes an array of promises and returns a promise that resolves when all of the promises passed into it are resolved.

Upvotes: 1

Related Questions