Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8537959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:01:36+00:00 2026-06-11T11:01:36+00:00

I am trying to use the node-facebook-sdk to make FB graph API calls using

  • 0

I am trying to use the node-facebook-sdk to make FB graph API calls using Node.JS

I want to get feed data from all of a users friends.
FB graph API only allows 50 friends per batch request, so I am making a series of batch FB.api() calls.

I am trying to store the FB.api() calls in an array, then using jQuery when() to call the deferred functions.

problems:

1) the function I pass to the .done() function is executed prematurely.
2) the console.log(feed) is executed after .done(), which to my knowledge shouldn’t happen.

$ = require('jQuery');


//maximum batch size request is 50. use 2-D array to store in buckets of 50 friends each.
                var numFriends = friends.data.length;
                var batch = [];
                var deferred = [];//array to hold all the requests
                var feed_dump = [];//where we collect all the feed data

            //initialize a bucket for each set of 50 friends
            for (var i = 0, ii = numFriends / 50; i < ii; i++) {batch.push([]);}

            //put corresponding requests in in buckets.
            for (var i = 0; i < numFriends; i++) {
                batch[Math.floor(i/50)].push({ method: 'get', relative_url: friends.data[i].id + '/feed?since=' + '-1 month'});//not sure if the date format will work user.last_updated
            }

            //make the multiquery request for each bucket
            for (var i in batch) {
                var bucket = batch[i];
                //nested FB api call

                deferred.push(FB.api('', 'post', {'batch' : bucket}, function(res){//res = array 50 friend feeds
                        if (!res || res.error) {
                            console.log(!res ? 'error occurred' : res.error); return;
                        }
                        for (var j in res) {
                            var feed = JSON.parse(res[j].body);
                            console.log(feed);
                            feed_dump.push(feed);//feed for each friend appeneded to dump
                        }
                }));
            }

            console.log('this should show up before graph api requests are made.');

            //jQuery when() function.
            $.when.apply(null, deferred).done(function() {
                console.log('hopefully feed_dump has been updated...');
                PySocket.emit('update_user_graph',JSON.stringify(feed_dump));
            });

How can I defer the batch FB.api() requests properly? Also, if someone can think of a better way to do this, please let me know; I am not that experienced with async javascript.

I guess a simpler form of my question is : how can I wait for multiple callback functions to finish?

Thanks a bunch.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:01:37+00:00Added an answer on June 11, 2026 at 11:01 am

    EDIT : Encapsulated callback count decrement : http://jsfiddle.net/3L9zc/32/

    function BatchAPICalls(){
        var calls = [];
        var self = this;
    
        self.push = function(){
            var args = [];
    
            for(var i = 1; i < arguments.length - 1; i++)
                args.push(arguments[i]);
    
            calls.push({
                fn: arguments[0],
                callback: arguments[arguments.length - 1],
                args: args
            });        
        };
    
        self.start = function(callback){
            var callbacksLeft = calls.length;
    
            function batchCallback(){
                callbacksLeft--;
                if(callbacksLeft == 0)
                    callback();
            }
    
            for(var i = 0; i < calls.length; i++){
    
                var call = calls[i];
    
                var batchItemCallback = function(){
                    call.callback.apply(this, arguments);
                    batchCallback();
                };
    
                call.fn.apply(this, call.args.concat([batchItemCallback]));            
            }
        };
    }
    
    
    
    var numFriends = friends.data.length;
    var batch = [];
    var feed_dump = [];
    
    for (var i = 0, ii = numFriends / 50; i < ii; i++) {
        batch.push([]);
    }
    
    for (var i = 0; i < numFriends; i++) {
        batch[Math.floor(i/50)].push({ 
            method: 'get', 
            relative_url: friends.data[i].id + '/feed?since=' + '-1 month'
        });
    }
    
    function done() {
        console.log('hopefully feed_dump has been updated...');
        PySocket.emit('update_user_graph',JSON.stringify(feed_dump));
    }    
    
    var batchCalls = new BatchAPICalls();
    
    for (var i in batch) {
        var bucket = batch[i];
    
        batchCalls.push(FB.api, 'call #' + i, 'post', {'batch' : bucket}, function(res){
    
            if (!res || res.error) {
                console.log(!res ? 'error occurred' : res.error); return;
            }
            for (var j in res) {
                var feed = JSON.parse(res[j].body);
                console.log(feed);
                feed_dump.push(feed);//feed for each friend appeneded to dump
            }
        });
    }
    
    console.log('this should show up before graph api requests are made.');
    
    batchCalls.start(done);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compile LESS from PHP and don't want to use node.js or
I'm trying to use Node.js to create a zip file from an existing folder,
i'm trying use facebook API to upload photo in my fan page. I downloaded
I'm trying to use node_load to pass php variables (as variables!) from one node
I'm trying to install Forever to use with Node.js. I'm installing it using 'npm
I'm trying to use GET variables to transfer some simple data but for some
I am trying to figure out how to use a node graph for processing
I am trying to use EXSLT node-set function from nodejs via node_xslt module. This
I am trying to use gmail smtp using node_mailer . I get following error
I'm trying to use quoted text in one of node's attributes: var network_json =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.