Saturday 22 April 2017

Asynchronous method in while loop with Graph API paged

I'm using facebook node sdk for node.js to get information from facebook user like feed and friends and all are working fine, but I'm having issue when the returned data is paged - I need to built something in recursive mode. Let me explain:

FB.api('/me/feed?limit=5000', {
    access_token: token
}, function(response) {
  // response is an object that could have as response.paging.next attribute
}); 

Limit doesn't working here, because it returns max 245 and returns paging object indicate to the next page.

Because the next call depends of the result of the before async call, I tried to do something like this:

// first call before
var hasNext = response.paging.next ? true : false;
while (hasNext){
    FB.api('/me/feed', {
        access_token: token
    }, function(response_paged) {
       response.data.concat(response_paged.data);
       // If do not have a next page to break the loop
       if (!response_paged.paging.next) hasNext = false;
    });
} 

The way as the next token Was obtained is not important for now

The point is I'm trying to do async calls in recursive mode, but is not working, this way I'm getting infinite loop.



via Lucas Costa

No comments:

Post a Comment