简体   繁体   English

AngularJS $ q:如何在链中并行完成诺言,然后并行执行?

[英]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". 下面的代码不起作用,我认为您可以在“ 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. 啊哈, $q.all( [ updateStatus, submitOrder] )始终返回立即解决的承诺。

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. [updateStatus,submitOrder]只是一个函数引用数组,在非promise的事物数组上调用$q.all总是返回立即解决的promise。

You need to call those functions, as (I'm assuming) those functions return promises when called. 您需要调用这些函数,因为(我假设)这些函数在被调用时会返回promises

$q.all takes an array of promises and returns a promise that resolves when all of the promises passed into it are resolved. $q.all接受一个$q.all数组,并返回一个promise,当传递给它的所有promise都被解析时,它就会解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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