title: determining the end of asynchronous operations javascript
body: <p>If I have a function that's passed this function:</p>
  
  <pre><code>function(work) {
     work(10);
     work(20);
     work(30);
  }
  </code></pre>
  
  <p>(There can be any number of <code>work</code> calls with any number in them.)</p>
  
  <p><code>work</code> performance some asynchronous activity—say, for this example, it just is a <code>timeout</code>. I have full control over what <code>work</code> does on the completion of this operation (and, in fact, its definition in general).</p>
  
  <p>What's the best way of determining when all the calls to <code>work</code> are done?</p>
  
  <hr>
  
  <p>My current method increments a counter when work is called and decrements it when it completes, and fires the <code>all work done</code> event when the counter is 0 (this is checked after every decrement). However, I worry that this could be a race condition of some sort. If that is not the case, do show my why and that would be a great answer.</p>