title: nodejs parallel callback design pattern
body: <p>I'm trying to find a good pattern to execute a bunch of parallel tasks.</p>
  
  <p>Let me define some task to exemplify. Tasks <code>a, b, c, d, e, f, g</code> execute as <code>a(function(er, ra){//task a returned, ra is result})</code>, so do <code>b</code> to <code>g</code></p>
  
  <p>There are also some tasks that should be execute after some task is done, let's call them <code>ab, bc, abc, bd, bcd, af, fg</code>, means when <code>a</code> and <code>b</code> has returned <code>ab(ra, rb)</code> should be executed at once, and when <code>b</code> and <code>c</code> returned, <code>bc(rb, rc)</code> should be executed at once, and if <code>a</code>, <code>b</code>, <code>c</code> all returned, <code>abc(ra, rb, rc)</code> should be executed.</p>
  
  <p>For the simplest case, if there is only <code>a</code> and <code>b</code>, I can do something like this:</p>
  
  <pre><code>(function(cb){
      var count = 2, _ra, _rb;
      function update(){if(--count == 0) cb(null, _ra, _rb)}
      a(function(er, ra){_ra = ra; update()});
      b(function(er, ra){_rb = rb; update()});
  })(function(er, ra, rb){
      ab(ra, rb);
  });
  </code></pre>
  
  <p>As you can see, <code>a</code> and <code>b</code> execute in parallel, and when both are done, <code>ab(ra, rb)</code> execute.</p>
  
  <p>But how can I do more things for a lot of parallel tasks?</p>