0

I have a plugin on my website that gets a user's Facebook login status. Using my own function facebookStatus() I want to return the user's Facebook ID. So far I have tried three approaches:

1.

console.log(facebookStatus()); // Should show the Facebook ID in the console

function facebookStatus() {
  facebookConnectPlugin.getLoginStatus(function(response) {    
    var result = response.authResponse.userID;
  }, function(error) {
    var result = error;
  });
  return result;
}

this yields

Uncaught ReferenceError: result is not defined

I am guessing this is due to the asynchronous nature of getLoginStatus().

2.

function facebookStatus() {
    facebookConnectPlugin.getLoginStatus(function(response) {    
        return response.authResponse.userID;
    }, function(error) {
        return error;
    }).then(function(response) {
        result = response;
    });
    return result;
}

Uncaught TypeError: facebookConnectPlugin.getLoginStatus(...).then is not a function

3.

facebookStatus().then(function(response) {
    console.log(response);
});

function facebookStatus() {
    return facebookConnectPlugin.getLoginStatus(function(response) {    
        return response.authResponse.userID;
    }, function(error) {
        return error;
    });
}

Uncaught TypeError: facebookStatus(...).then is not a function

PS: I am using jQuery, if it helps.

Community
  • 1
  • 1
Tonald Drump
  • 1,227
  • 1
  • 19
  • 32
  • 1
    1. No, the error occurs because `result` is not defined. `var` introduces variables in the function scope, so outside of the function in which you introduce the variable, it's not defined. 2. and 3. `then` is a function which is defined for Promises, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then . – Michael H. Mar 24 '18 at 11:23
  • Thanks, I have now tried `return new Promise(facebookConnectPlugin.getLoginStatus(function(response) { ... }`, but that returns `Uncaught TypeError: Promise resolver 1 is not a function` – Tonald Drump Mar 24 '18 at 13:54
  • 1
    Yes, because that's not how promises work. You would have to rewrite the function to work. – Michael H. Mar 24 '18 at 19:51
  • Is there no way to take that `facebookConnectPlugin.getLoginStatus()` and 'wrap' or turn it into a promise function? – Tonald Drump Mar 24 '18 at 20:18

0 Answers0