0

So I have this facebook login javascript code and say I want to make changes to a global variable that goes inside this function, is that possible? so I want to make something like this:

var Name;
window.fbAsyncInit = function() {
      FB.init({
        appId      : '2295720594086082',
        cookie     : true,
        xfbml      : true,
        version    : 'v3.2'
      });

      // FB.AppEvents.logPageView();   

    FB.getLoginStatus(function(response) {
        statusChangeCallback(response);

        FB.api('/me?fields=name,email', function(response) {
          console.log(response);
          Name = response.name;
        })
    });
console.log(Name);

But I always get that Name is undefined, I know that this function is Asynchronous but I don't know how to use this....

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
C. Cristi
  • 569
  • 1
  • 7
  • 21
  • Have you tried to place the console.log call below "Name = response.name" ? Name will only be defined when the callback is called (the response received). – Pedro leal Feb 14 '19 at 20:50
  • @Pedroleal Yes and it works, but when I try to call it outside the function it doesn't work anymore – C. Cristi Feb 14 '19 at 20:52
  • That's because it's an asynchronous function. Read the answer of the question that yours was marked as duplicate. – Pedro leal Feb 14 '19 at 21:38

0 Answers0