Extract data from anonymous functions

Due to the complexity of this application, I need to wrap Facebook API calls, for example.

//In main file, read is always undefined
var read = fb_connect.readStream();

// In fb_wrapper.js
function readStream () {
    var stream;

    FB.api('/me/feed', {limit:10000}, function (response) {
        stream = response.data;
    });

    return stream;
}

I know that due to the asynchronous nature of the call, the rest of the function readStream()will return stream(which doesn't matter). I am having trouble finding a way to get data from the callback functions area and return to a higher area. The FB API call returns in order (I debugged it a hundred times), but getting this response data has been a battle so far.

If anyone has any suggestions, that would be very appreciated. I searched for Facebook jQuery plugins (maybe like ready-made wrappers) with little luck.

+3
source share
1 answer

, , . , , api . , FB.api ( ).

, async. , INSIDE , FB.api. "" .

FB.api('/me/feed', {limit:10000}, function (response) {
    var stream = response.data;
    // Do your processing here, not outside!!!
});

:

function handlerFunction(response) {
   // Do your processing here
}

FB.api('/me/feed', {limit:10000}, handlerFunction);
+2

All Articles