Make FB.api () a synchronous call

I am creating a fQuery API on top of the FB javascript SDK. And so far everything has been working fine, but now I am stuck in FB.api calls.

Actually, I am trying to load a custom facebook object ie "/ me" using the FB.api function.

function somefunc() { var r = fQuery.load(selector); //selector = "me" return r; } fQuery.load = function( selector ) { fQuery.fn.response = ""; return FB.api( "/" + selector, function (response) { // we get response here. }); } 

Is it possible to return the answer or we can synchronize it. I tried many ways to work, but could not succeed.

Please provide suggestions.

+7
source share
4 answers

If you think about it, you really don't want to make it synchronous. Javascript is single-threaded in nature, creating what is asynchronous synchronous will enable the thread to β€œfreeze” until the asynchronous call returns.

Even if you can do this, you do not want to, believe me.

Recycle your code to work with asynchronous character, and not fight with it. you will create better applications, get happier users and at the same time you can become a better encoder.

+8
source

As commented elsewhere, a synchronous call is useful if you want to open a pop-up after a successful response, since browsers often block pop-ups that are not the result of direct user action.

You can do this by manually using the Open Graph API with JavaScript (or jQuery according to the example below), and not using the Facebook JS SDK.

eg. upload a photo via the Open Graph API, and then ask the user to add it as an image of their profile using a pop-up window without blocking the pop-up window:

 $.ajax({ type: 'POST', url: 'https://graph.facebook.com/me/photos', async: false, data: { access_token: '[accessToken]',//received via response.authResponse.accessToken after login url: '[imageUrl]' }, success: function(response) { if (response && !response.error) { window.open('http://www.facebook.com/photo.php?fbid=' + response.id + '&makeprofile=1'); } } }); 
+4
source

You probably want to call FB.api in an iteration of the for loop, if so, you need its correct solution, which exists when using Closures . Please read my answer here , indicated in another question

-one
source

My solution was to make a recursive call until I got what I need from FB.api

-5
source

All Articles