tags:

views:

32

answers:

1

This sounds like a weird title and probably not stated too well. But here's the issue.

I've got this method being called:

function BindFacebookAlbumAndPhotoData() 
{
    GetAllFacebookAlbums(userID, accessToken, function(aAlbums) 
    {
        if (aAlbums === null || aAlbums === undefined) 
        {
            // TODO: Need to handle this
            return;
        }

        var defaultAlbumID = aAlbums[0].id;

        BindFacebookAlbumDropdownList(aAlbums, defaultAlbumID);

        BindFacebookThumbnails(userID, accessToken, defaultAlbumID, photosContainerDivID);

        SetSelectDefaultOption(defaultAlbumID);
    });
}

So here's the GetAllFacebookAlbums Method:

function GetAllFacebookAlbums(userID, accessToken, callbackFunction)
{
    var aAlbums = [];
    var uri = "/" + userID + "/albums?access_token=" + accessToken;

    // this is an async call so code after this will fire at the same time
    FB.api(uri, function(response) 
    {
        // check for a valid response
        if (!response || response.error) 
        {
            callbackFunction(albums);
        }

        for (var i = 0, l = response.data.length; i < l; i++) 
        {
            //do whatever
        }
    });

    // wait for the FB.api call to run a bit before calling code below
    window.setTimeout(callbackFunction(aAlbums), 5000);
}

What's happening is this line is being called before the setTimeout it seems:

var defaultAlbumID = aAlbums[0].id;

the whole reason I put that timeout in there is so I can wait for the FB.api call to finish. Then call the callback after I know I have data. So how in the world are those lines in my callback being called when I have definitely not seen a 5 second delay before that callback is called?

+5  A: 

This line right here:

window.setTimeout(callbackFunction(aAlbums), 5000);

You're calling the function right there in the setTimeout() call. Wrap it in another function:

window.setTimeout(function() { callbackFunction(aAlbums); }, 5000);

Now, that said, the whole setup doesn't really make much sense. You've already got code sitting in a place that you know will be called as soon as the data is ready. That's the code in the Facebook API callback you've got. Why not just call your "callbackFunction" from in there?

Pointy
well spotted ...
Preet Sangha
"You've already got code sitting in a place that you know will be called as soon as the data is ready". Not necessarily. The FB.api call is an async call so my callback (callbackFunction(aAlbums)) is being called at the same time my FB.api call is being called.
CoffeeAddict
"That's the code in the Facebook API callback you've got. Why not just call your "callbackFunction" from in there?". I've tried that already in the past, and got all sorts of hell. Because if you do that, then what happens is the callback for FB.api never gets hit/run if that last callback to callbackFunction(aAlbums) is called from within the FB.api callback itself
CoffeeAddict
??? huh? No, I don't think that's correct. The whole point of "complete" APIs like that is to give you a way to run code at the completion of some asynchronous process. Nobody uses timeouts to deal with that, because it's completely unreliable and it wastes the user's time (potentially).
Pointy
I'll edit my post to explain further.
Pointy
weird, now it works...as in before if I were to call the callback from within my FB.api it was just causing me problems.
CoffeeAddict
Yes I know that's the whole point...but at the time I was trying that it was causing issues by doing it that way. Trust me. I was not getting the response back, but it was most likely a different problem then it looks like. So I put it back in there as you suggested and yes, that was my original intent in the first place, but I got away from it because it was causing me issues in the past for whatever reason related or not.
CoffeeAddict