Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value variable outside fb.api with callback

I know the problem with ajax and return variables, I was reading in stackoverflow about this, I know that I have to use a callback function, but in my case didn't work, of course something I'm making wrong

My code is this:

var id_user=get_id_user_login();//undefined??????????

function get_id_user_login(){
    FB.api(                                                
         '/me',                     
          {fields:'id'},
          function(response){//callback       
                 console.log(response.id);//OK
                 return response.id;
           }
     );             
};
like image 561
francis Avatar asked Dec 11 '12 14:12

francis


1 Answers

You can´t just return the value because it´s asynchronous. Try this:

function customFunction(id) {
    console.log(id);
}

function get_id_user_login(){
    FB.api(                                                
         '/me',                     
          {fields:'id'},
          function(response){   
                 customFunction(response.id);
           }
     );             
};

get_id_user_login();
like image 112
andyrandy Avatar answered Oct 21 '22 14:10

andyrandy