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() ;
}
}) ;
}
}