Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looks like you should use the <code>Q.all</code> function to create a master promise corresponding to when all the getSizeSettings promises are fufilled.</p> <p><a href="https://github.com/kriskowal/q#combination" rel="nofollow">https://github.com/kriskowal/q#combination</a></p> <pre><code>var ps = []; for (var i=0, l=pages.length; i&lt;l; i++) { ps[i] = getSizeSettingsFromPage(pages[i], calculateRatio); } Q.all(ps).then(function(){ callWhenReady() }) </code></pre> <p>Most promise libraries should provide a similar method to do this kind of synchronization. If you ever come across one that does not what you could do is hook each individual promise to a callback that increments a shared counter when its called. When your counter reaches <code>n</code> you know that you already resolved all promises so you can have the incrementor callback call the "real" callback as well.</p> <pre><code>//If you did not have Q.all available //Or had to code this without a promise library var to_go = pages.length; for (var i=0, l=pages.length; i&lt;l; i++) { getSizeSettingsFromPage(pages[i], calculateRatio) .then(function(){ to_go--; if(to_go == 0){ callWhenReady() } }); } </code></pre> <p>Note that in these cases until now the async calls are allowed to run parallel. If you need them to run sequentially then usually the only way is to rewrite the for loop as a recursive function</p> <pre><code>var go = function(i){ if(i&gt;=pages.length){ return call_next_step() }else{ return do_ith_calculation(i) .then(function(){ return go(i+1) }) } }; go(0); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload