0

I am assigning a function as a part of an object and later need to call that function as a promise. But even defined as a promise I am getting an error on the call method implying it doesn't recognize it as a promise.

If I just do the appObj.loginApp() (which in this case is actually the loginTwitter() function) the function works as intended, however I need to get the true/false back from the function. I think I correctly defined the function as a promise so I am not understanding why its failing on the .then in the appObj.loginApp().then(function() { ... }.

what am I doing wrong? Can a promise even be called this? I am assuming it can but have had a hard time finding examples of others having done it this way.

function setSocialApp(app) {
  if (app == "twitter") {
    var appObj = {
      ios : "twitter://",
      and : "com.twitter.android",
      loginApp : function (a,b,c) { loginTwitter(a,b,c) },
      logoutApp : function () { logoutTwitter() },
      disableApp : function () { disableTwitter() },
      appName : "Twitter",
      appDB : "social_twitterLogin"
    }
  } else if (app == "instagram") {
    ...
  } else if (app == "facebook") {
    ...
  }
  return appObj ;
}

function loginTwitter() {
  return new Promise ( function (resolve) {
    TwitterConnect.login(
      function(result) {
        console.log('Successful login!');
        setDB("social_twitterLogin",true) ;
        setDB("social_twitterUserID",result.userId);
        setDB("social_twitterUserName",result.userName) ;
        setDB("social_twitterSecret",result.secret) ;
        setDB("social_twitterToken",result.token) ;
        console.log(result);
        resolve (true) ;
      },
      function(error) {
        console.log('Error logging in');
        disableTwitter() ;
        console.log(error);
        resolve (false) ;
      }
    );
  }) ;
}

   function enableApp(app) {
      var appObj = setSocialApp(app) ;  // get app object info
      if (socialItem[appObj.appDB] == false) {
        appObj.disableApp() ;
      } else {
        checkSocialApp(appObj).then(function(response) {  // check if app exists
           if (response == true) {
              console.log("CheckApp True") ;
              if (socialItem[appObj.appDB] == true) {  // toggled on
                console.log("socialItem True") ;
                appObj.loginApp().then(function () {  // login failed
                  // THIS IS NOT WORKING, fails on `then`
                  socialItem[appObj.appDB] = false ;  // turn toggle off
                } ;
              } else {            // user toggled off
                console.log("socialItem False") ;
                appObj.logoutApp() ;
              }
            } else if (response == false) {  // social app IS NOT installed
              console.log("CheckApp False") ;
              socialItem[appObj.appDB] = false ;  // turn toggle off
              appObj.disableApp() ;
            }
          }) ;
        }
      }
rolinger
  • 2,787
  • 1
  • 31
  • 53
  • 3
    your loginApp doesn't return anything – Patrick Evans Jan 28 '18 at 21:58
  • 1
    `return` the `Promise` from the functions `loginApp : function (a,b,c) { return loginTwitter(a,b,c) }, logoutApp : function () { return logoutTwitter() }, disableApp : function () { return disableTwitter() }` – guest271314 Jan 28 '18 at 22:00
  • Thanks for the response. however I don't see how this is a duplicate of that other post. that is just a straight promise. My issue is a promise wrapped in a function assigned to an object. Its not as straight forward as simply calling a standard promise. – rolinger Jan 28 '18 at 22:07
  • 1
    @rolinger How is the Answer to the present Question different from the Answer to the linked Question? In either case the `Promise` or value needs to be `return`ed, else get `undefined` at `.then()` or an error `Uncaught TypeError: Cannot read property 'then' of undefined` – guest271314 Jan 28 '18 at 22:09
  • Apologies. Not understanding the solution I didn't see how that post was relevant, but now I understand. I completely over looked such a basic thing...nothing was being returned. duh. – rolinger Jan 28 '18 at 22:13

0 Answers0