title: Yet another 'synchronize FB apis' post
body: <p>I'm using FB apis and having troubles in making this code work:</p>
  
  <pre><code>DoSomething()
  {
      var validFriend = [];
  
      EnumerateFriends(function(result)
      {
          for every friend in result
          {
              QuerySingleFriend(result.friendId, function(result)
              {
                  ...
  
                  validFriend += thisguy;
                  ...
  
              });
          }
      });
  
      writeout("Your valid friends are: "+validFriend);
  
  }
  
  EnumerateFriends(callback)
  {
      FB.api("give me my friends", callback);
  }
  
  QuerySingleFriend(friendId, callback)
  {
      FB.api("give me this guy", callback);
  }
  </code></pre>
  
  <p>The above pseudocode is pretty simple and straightforward but the "writeout" statement gets executed before everything is finished due to the asynchronous nature of the fb apis callback functions.</p>
  
  <p>Any idea on how to synchronize this? I tried with global variables but isn't nor an elegant nor a practical solution</p>