Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use an FB.api(JS SDK) response outside of the callback function?
    primarykey
    data
    text
    <p>I am not having any issues with logging in or even calling the api, I just have an issue with getting the response outside of the api callback. I know that it runs asynchronously so I would like to put it in a function that would return the response. Here is my idea</p> <pre><code>//What I would like to be able to do function fbUser(){ FB.api('/me', function(response){ //this logs the correct object console.log(response); }); //How do I get the response out here? return response; } </code></pre> <p>I would like to call the /me api function once in the beginning and then pass it around to my view objects (I just use the response inside of Backbone Views) and depending on what is needed make other api calls. I currently have certain things working by calling the view from inside of the callback</p> <pre><code>//What I am doing now, but I lose the ability to pass anything other than the //the current response to this function/View FB.api('/me', function(response){ var newView = new facebookView({model: response}); }); </code></pre> <p>I orginally was trying this, but because the api call is asynchronous I had issues with things being undefined</p> <pre><code>//What I started with but had async issues var fbResponse; FB.api('/me', function(response){ fbResponse = response; }); //I would then try and use fbResponse but it would be undefined </code></pre> <p>I lose the first response when I make the second. For example my first api call is to /me to get the user info. I can then call /your-fb-id/photos and get photos, but if I make the call to another function inside of the photo api callback I only can reference that response I lost the original /me response. If I could get the response out of the callback then I would be able to pass it as needed. I understand that response is only valid inside of the callback, so how do I make it valid outside of the callback while taking into account it's asynchronousness?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload