简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM