title: JavaScript control flow in node/redis: returning from inside callback?
body: <p>Newbie question. Why is this JavaScript function returning undefined?</p>
  
  <pre><code>var redis = require("redis"), client = redis.createClient();
  function generatePageUrl() { 
      var randomStr = randomInt.toString(32);
      // Check whether this URL is already in our database;
      client.smembers("url:" + randomStr, function (err, data ) {
          if (data.length != 0) {
              // URL already in use, try again
              return getPageUrl();
          }
          return randomStr;
      });
  }
  var page_url = generatePageUrl();
  // add it to the database, etc
  </code></pre>
  
  <p>I guess it must be getting to the end and returning before it reaches the inside of <code>client.smembers</code>. </p>
  
  <p>But I really need to check the contents of the Redis set before it returns: can I get it to return from inside the callback? If not, what can I do?</p>
  
  <p>Also, advice on the way I've used this function recursively would be welcome - I'm not sure it's completely sensible :)</p>
  
  <p>Thanks for helping out a newcomer.</p>