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 8973161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:23:04+00:00 2026-06-15T18:23:04+00:00

I am trying to count friends’ gender with Facebook JavaScript API. The problem is

  • 0

I am trying to count friends’ gender with Facebook JavaScript API. The problem is FB SDK is based on async call so I cannot fire an action after all the queries are done.

For example, here is code to count how many male/female friends I have. But the last line won’t work as it can get executed while FB.api() is still running.

What’s the best way to handle this?

FB.api('/me/friends', function(response) {
    if(response.data) {
        var genderCount = {};

        $.each(response.data, function(index, friend) {
            FB.api('/' + friend.id, function(frienddata) {
                if (frienddata.gender) {
                    if (genderCount[frienddata.gender]) {
                        genderCount[frienddata.gender]++;
                    }
                    else {
                        genderCount[frienddata.gender]=1;
                    }
                }
            });
        });
});

// later do something with genderCount

alert('I have ' + genderCount['male'] + ' male friends'); // won't work
  • 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-15T18:23:05+00:00Added an answer on June 15, 2026 at 6:23 pm

    First of all, you probably want to use a more efficient fql query, such as

    SELECT name, sex, uid 
    FROM user 
    WHERE uid in (
      SELECT uid2 
      FROM friend 
      WHERE uid1 = me()
    )
    

    Simply pass this to graph.facebook.com/fql?q=SELECT%20%name....

    Then, move the alert into the response handler, after you have completed summing up the numbers.

    FB.api('/fql', 
      {q: "SELECT name, sex, uid FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me())"}, 
      function (response) {
        if (response.data) {
            var genderCount = {
                male: 0,
                female: 0,
                unknown: 0
            };
            response.data.forEach(function (row) {
                genderCount[row.sex || 'unknown']++;
            });
            alert(JSON.stringify(genderCount));
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get friends count via my account using Facebook API. Its working
Using Facebook graph API, I am trying to retrieve number of mutual friends 2
What I'm trying to do is a counter of facebook friends in php, so
I am trying to grab the latest links shared by a user's facebook friends
I'm trying to retrieve birthday data of facebook friends, but it's returning null. I'm
I am developing an ASP.NET application. I implemented Facebook JavaScript API in my application,
Trying to count how many elements within the array are not equal to 0,
I am trying to count the number of times a user has clicked on
I am trying to count the occurrences of certain dates in my MySQL table
I'm trying to count the number of times that -- occurs in a string.

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.