title: Control flow issue with node/redis and callbacks?
body: <p>Please could I ask for some advice on a control flow issue with node and redis? (aka Python coder trying to get used to JavaScript)</p>
  
  <p>I don't understand why <code>client.smembers</code> and <code>client.get</code> (Redis lookups) need to be callbacks rather than simply being statements - it makes life very complicated.</p>
  
  <p>Basically I'd like to query a set, and then when I have the results for the set, I need to carry out a get for each result. <em>When I've got all the data</em>, I need to broadcast it back to the client. </p>
  
  <p>Currently I do this inside two callbacks, using a global object, which seems messy. I'm not even sure if it's safe (will the code wait for one <code>client.get</code> to complete before starting another?). </p>
  
  <p>The current code looks like this:</p>
  
  <pre><code>var all_users = [];
  // Get all the users for this page.
  client.smembers("page:" + current_page_id, function (err, user_ids ) {
    // Now get the name of each of those users.
    for (var i = 0; i &lt; user_ids.length; i++) {
       client.get('user:' + user_ids[i] + ':name', function(err, name) {
         var myobj = {};
         myobj[user_ids[i]] = name;
         all_users.push(myobj);  
         // Broadcast when we have got to the end of the loop, 
         // so all users have been added to the list - 
         // is this the best way? It seems messy.  
         if (i === (user_ids.length - 1)) {
             socket.broadcast('all_users', all_users); 
         }
       });       
     }
   });
  </code></pre>
  
  <p>But this seems very messy. Is it really the best way to do this? How can I be sure that all lookups have been performed before calling <code>socket.broadcast</code>?</p>
  
  <p><em>scratches head</em> Thanks in advance for any advice.</p>