等待多承诺为一组,并单独在角? [英] Waiting on multiple promises as a group and singly in angular?

查看:143
本文介绍了等待多承诺为一组,并单独在角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Restangular做服务器的几个同时呼叫。 Restangular返回承诺为每一个动作,他们中的每一个都需要做其特定的一些回报,而一旦所有3个是完整和。然后的功能已经完成,我需要执行其他功能。

这是做承诺,在这种情况下正确的方法是什么?

 变种A,B,C;A = service1.getList(...),那么(函数A(数据){做一些事情; $ log.debug('A');返回'A';});
B = service2.getList(...),那么(函数B(数据){做一些事情; $ log.debug('B');返回'B';});
C = service3.getList(...),那么(函数C(数据){做一些事情; $ log.debug('C');返回'C';});$ q.all([A,B,C]),那么(函数WRAPUP(值)。{$ log.debug(值);做功能A,B和C以上完成后的东西;}

我不知道是否$ q.all()将在A,B和C的最后运行,或者它可能在最后的功能前赛。将结束语()总是最后调用?

请注意在A,B和C是一个承诺,b和c完成后调用的函数。

的数值数组传递给WRAPUP似乎是从A,B和C的返回值然而,在我的控制台,功能日志10次,然后函数B登录10次,然后函数C和功能WRAPUP是这两个记录10次(交替)。

10倍的记录看起来像是被打破......和害怕竞争条件似乎是一个可能性....

有人能解释一下是怎么回事?


解决方案

  

这是做承诺,在这种情况下正确的方法是什么?


是的,是的。


  

10倍的记录看起来像是被打破......和害怕竞争条件似乎是一个可能性....


  
  

有人能解释一下是怎么回事?


它实际上不破。像你这样的建议 $ q.all 等待所有承诺才能完成。

  $ q.all([A,B,C,D]),然后(函数(){
     //是的,之后所有承诺的履行了这一只运行
 });

下面是发生了什么:

您已经运行10次的循环:


  • 系统从整个循环中的所有履行瞬间来电。

  • b从整个循环全部履行即刻调用。

  • 调用C议决

    • 一,B,C为一个(已经解决)的B(已经解决)的该实例的 $ q.all 组成的承诺,现在C - 履行


  • 下一个C调用议决

    • 一,B,C为一个(已经解决)的B(已经解决)的该实例的 $ q.all 组成的承诺,现在C - 履行


等。

您的唯一保证就是循环内的该实例 $ q.all(的的[A,B,C] 将履行之后的本地的A,b和C。没有别的,仅此而已。

如果你想勾上所有的返回值的总和诺言,你当然可以对他们的所有十个执行 $ q.all

I'm using Restangular to do several simultaneous calls to the server. Restangular returns promises for each action, each one of them needs to do something with its specific return, and once all 3 are complete and their .then functions have completed, I need to perform another function.

Is this the right way to do promises in this instance?

var a, b, c;

a = service1.getList(...).then(function A(data) { do something; $log.debug('a'); return 'a';});
b = service2.getList(...).then(function B(data) { do something; $log.debug('b'); return 'b';});
c = service3.getList(...).then(function C(data) { do something; $log.debug('c'); return 'c';});

$q.all([a,b,c]).then(function wrapUp(values) { $log.debug(values); do something after functions A, B,and C above have finished;}

I don't know if $q.all() will run at the very end of A,B, and C, or if it could race in front of the last function. Will wrapUp() ALWAYS be called last?

Note the A,B, and C are the functions called after promises a,b, and c are completed.

The values array passed to wrapUp appears to be the the return values from A,B and C. However, in my console, functions A logs 10 times, and then function B logs 10 times, then function C and function wrapUp are both logging 10 times (alternating).

The 10x logging seems like something is broken....., and the feared race condition appears to be a possibility....

Can someone explain what is going on here?

解决方案

Is this the right way to do promises in this instance?

Yes, it is.

The 10x logging seems like something is broken....., and the feared race condition appears to be a possibility....

Can someone explain what is going on here?

It's actually not broken. Like you suggest $q.all waits for all promises to complete.

In:

 $q.all([a,b,c,d]).then(function(){
     // yes, this only runs after all of the promises fulfilled
 });

Here is what happens:

You have a loop that runs 10 times:

  • A calls from the entire loop all fulfill instantly.
  • B calls from the entire loop all fulfill instantly.
  • C call resolves
    • The promise composed of the $q.all for that instance of a,b,c for the a (already resolved) the b (already resolved) and now the c - fulfilled.
  • Next c call resolves
    • The promise composed of the $q.all for that instance of a,b,c for the a (already resolved) the b (already resolved) and now the c - fulfilled.

And so on.

The only guarantee you have is that inside the loop that instance of $q.all([a,b,c] will fulfill after its local a,b and c. Nothing else, nothing more.

If you want to hook on the aggregate promise of all the return values, you can of course perform a $q.all on all ten of them.

这篇关于等待多承诺为一组,并单独在角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆