Q:

Yet another 'synchronize FB apis' post

I'm using FB apis and having troubles in making this code work:
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);
}

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.
Any idea on how to synchronize this? I tried with global variables but isn't nor an elegant nor a practical solution

A:

Solved with concatenation and a global variable

