0

My questions are below, after the code:

var CustomUtils = {
    _constPageID: 12345,

    // This is called with the results from from FB.getLoginStatus().
    _statusChangeCallback: function(response) {
        console.log('statusChangeCallback');
        console.log(response);
        var callback = function(data) {
            alert('callback: ' + data); //actually I want the data here to be the return value of my main function below (at the end)
        };

        if (response.status === 'connected') {
            // Logged into your app and Facebook.
            CustomUtils._getToken(callback);
        } 
    },
    /* main function to get a token */
    getAndReturnToken: function() {

        $.ajaxSetup({ cache: true });
        $.getScript('https://connect.facebook.net/en_US/sdk.js', function(){
            FB.init({
                appId: '....appid......',
                autoLogAppEvents : true,
                xfbml            : true,
                version: 'v3.0' 
            });     
            FB.getLoginStatus(CustomUtils._statusChangeCallback);

        });
    },
};

For this code:

  1. I get a TypeError: this._statusChangeCallback is not a function[Learn More] in the console if I try to replace CustomUtils with this in the above code. Using CustomUtils works - why can't I use this? Note that this._constPageID can be used just fine (I haven't pasted all my code in this object but it works). It's just the functions within this object that cannot refer to each other using this, it seems.

  2. The getAndReturnToken function is actually supposed to return a token value, after certain API requests are completed (which actually return a token). The requests are asynchronous, and my newbie questions around this are answered here: How to return value from an asynchronous callback function? However, I don't want to "do more work" in my call back...

I simply want to return the token so it can be used elsewhere. The problem is the API request is asynchronous and we don't know when it will return (by logic), but without a token my code can't do much else as it still needs to use the API for further requests, with that token. Should I use some sort of event listener - or populate the results in my callback function into a variable, and if that variable has a value then return it? Sorry, I'm confused on the 'normal' way to go forward here.

rishijd
  • 1,294
  • 4
  • 15
  • 32
  • 1
    Ask **one** question per question, not two. If they relate to the same code, probably one then the other (as frequently the first answer leads you to the second yourself). – T.J. Crowder May 10 '18 at 14:37
  • 1
    https://stackoverflow.com/questions/41496958/this-does-not-work-properly-in-another-event-im-clueless-as-to-why/41496969#41496969 – Scott Marcus May 10 '18 at 14:37
  • 1
    Your first question is answered [here](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback). Your second, [here](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – T.J. Crowder May 10 '18 at 14:38
  • @T.J.Crowder thank you, I'm just very confused. For the second question, if I use callbacks (which is what I'm trying to do), how would my `getAndReturnToken` actually `return` the value from the callback? For the first question I'm not sure if I'm currently doing the right thing though, by using the object again simply. I'll work through that whole SO answer in the other link. – rishijd May 10 '18 at 14:52
  • @rishijd: *"how would my getAndReturnToken actually return the value from the callback"* It wouldn't, and can't; see that question's answers for details. *"For the first question I'm not sure if I'm currently doing the right thing though, by using the object again simply."* Using `CustomUtils._statusChangeCallback` there is fine, your object is a singleton, so no harm in using the one variable you assign it to. :-) Happy coding! – T.J. Crowder May 10 '18 at 14:53
  • @T.J.Crowder thanks a lot :) For the second question, sorry, and this probably deserves a new SO post if you agree, the way I see it - my app depends on getting the token for the Facebook API. For example, reading page info, reading posts, creating a new post - these can only happen once the token is fetched using the functions above. So depending on what the user wants to do (what page is being visited on the web app), do I detect that within a master callback function (e.g. a global routing/mode variable), and then call the appropriate functions depending on where we are in the app? – rishijd May 10 '18 at 14:59
  • @ScottMarcus thank you! – rishijd May 10 '18 at 15:07
  • 1
    @rishijd: The exact way you handle it will depend on your application structure, but fundamentally, you'll have to handle the fact that `getAndReturnToken` **can't** literally return the token; instead, it'll have to return a promise or take a callback or something. Your app will have to have a state where it's waiting for that token to come back asynchronously. – T.J. Crowder May 10 '18 at 15:37
  • 1
    @T.J.Crowder thank you so much for your help. For now I'm doing this with callbacks, although the better/next way forward would be with a promise. Your comments really helped. – rishijd May 11 '18 at 02:22

0 Answers0