Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make FB.api() calls synchronous

I am creating fQuery API on top of FB javascript SDK. And till now everything worked fine, but i got stuck in FB.api calls now.

Actually, I am trying to load facebook user object i.e. "/me" using 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 response or can we make it sync call. I have tried many ways to work around but could not get success.

Please provide suggestions.

like image 484
Kevindra Avatar asked Mar 09 '11 12:03

Kevindra


1 Answers

If you think about it, you don't really want to make it synchronous. Javascript is single threaded by nature, making something that is asynchronous synchronous, would involve "freezing" the thread until the asynchronous call returns.

Even if you could do it, you don't want to, trust me.

Redesign your code to work with the asynchronous nature instead of fighting it. you will create better applications, have happier users and become a better coder all at the same time.

like image 58
Martin Jespersen Avatar answered Nov 12 '22 10:11

Martin Jespersen