title: JS Closures, Redis, loop, Async :: empty array
body: <p>I give up on this. May some of the wise stackoverflow monks please fix my bugs?</p>
  
  <p>Code is self explaining. Client sends room names, server does a redis lookup and pushes valid rooms to the array. After adding all the rooms, the list should be emitted to the client. </p>
  
  <p>Problem is closure, async etc. based. I understand the problem but cannot get a workaround because the array needs to remain inside the function. Tricky.</p>
  
  <p>Code:</p>
  
  <pre><code>function roomList(socket){
  
    var roomlist = [], rooms = getRooms(), p = /pChannel_/;
  
    redis.select(7, function(err,res){
  
      for (var k in rooms){
  
        if(rooms[k] != '' &amp;&amp; p.test(rooms[k])){
  
          var key = 'channel:'+rooms[k];
  
          redis.hgetall(key, function (err, reply) { 
  
            if(reply){ 
              var c = io.sockets.manager.rooms[rooms[k]];
              roomlist.push( Array(reply['name'],c.length,reply['icon']) );
            }
            else { console.log('nothing found'); }
  
          });
  
        }
  
      }
  
      // here be dragons
      console.log(roomlist);
      socket.emit('roomList', roomlist);
  
    });
  
  }
  </code></pre>
  
  <p>Thanks.</p>