作为一个群体等待多个承诺并单独处于角度?

时间:2022-05-01 12:17:52

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.

我正在使用Restangular同时对服务器进行几次调用。 Restangular返回每个动作的promise,每个动作都需要对其特定的返回做一些事情,一旦所有3个完成并且它们的.then函数完成,我需要执行另一个函数。

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?

我不知道$ q.all()是否会在A,B和C的最后运行,或者它是否可以在最后一个函数前面竞争。 wrapUp()总是最后被调用吗?

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

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

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

传递给wrapUp的values数组似乎是来自A,B和C的返回值。但是,在我的控制台中,函数A记录10次,然后函数B记录10次,然后函数C和函数wrapUp都记录10次(交替)。

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

10x伐木似乎有些东西被打破.....,恐惧的种族情况似乎是可能的....

Can someone explain what is going on here?

有人能解释一下这里发生了什么吗?

作为一个群体等待多个承诺并单独处于角度?

1 个解决方案

#1


7  

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....

10x伐木似乎有些东西被打破.....,恐惧的种族情况似乎是可能的....

Can someone explain what is going on here?

有人能解释一下这里发生了什么吗?

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

它实际上没有被打破。就像你建议$ q.all等待所有承诺完成。

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:

你有一个运行10次的循环:

  • A calls from the entire loop all fulfill instantly.
  • 来自整个循环的调用都立即完成。

  • B calls from the entire loop all fulfill instantly.
  • 来自整个循环的B调用都立即完成。

  • 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.
    • 承诺由$ q.all组成,a,b,c为a(已经解决)b(已经解决),现在是c - 履行。

  • C调用解析由$ q.all组成的承诺,a,b,c的实例为a(已经解析)b(已经解析),现在是c - 履行。

  • 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.
    • 承诺由$ q.all组成,a,b,c为a(已经解决)b(已经解决),现在是c - 履行。

  • 下一个c调用解析由$ q.all组成的承诺,a,b,c的实例为a(已经解析)b(已经解决),现在c - 履行了。

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.

唯一的保证是在循环内部$ q.all([a,b,c]的实例将在其本地a,b和c之后实现。没有别的,仅此而已。

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.

如果你想挂钩所有返回值的聚合承诺,你当然可以对它们中的所有10个执行$ q.all。

#1


7  

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....

10x伐木似乎有些东西被打破.....,恐惧的种族情况似乎是可能的....

Can someone explain what is going on here?

有人能解释一下这里发生了什么吗?

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

它实际上没有被打破。就像你建议$ q.all等待所有承诺完成。

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:

你有一个运行10次的循环:

  • A calls from the entire loop all fulfill instantly.
  • 来自整个循环的调用都立即完成。

  • B calls from the entire loop all fulfill instantly.
  • 来自整个循环的B调用都立即完成。

  • 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.
    • 承诺由$ q.all组成,a,b,c为a(已经解决)b(已经解决),现在是c - 履行。

  • C调用解析由$ q.all组成的承诺,a,b,c的实例为a(已经解析)b(已经解析),现在是c - 履行。

  • 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.
    • 承诺由$ q.all组成,a,b,c为a(已经解决)b(已经解决),现在是c - 履行。

  • 下一个c调用解析由$ q.all组成的承诺,a,b,c的实例为a(已经解析)b(已经解决),现在c - 履行了。

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.

唯一的保证是在循环内部$ q.all([a,b,c]的实例将在其本地a,b和c之后实现。没有别的,仅此而已。

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.

如果你想挂钩所有返回值的聚合承诺,你当然可以对它们中的所有10个执行$ q.all。