Q:

javascript Convert standard function to a promise

I have a standard function that successfully checks to see if a particular app is installed.  But that function takes a few moments to execute and I need to bind it into a promise.then(function (response) {  ...} because the app checker takes too long to execute...causing an async issue with the intended response from the app check function.  But I can't get it work.
checkSocialApp only returns a true or false:
function checkSocialApp(objApp) {
  if (device.platform == "iOS") {
    var scheme =  objApp.ios;
  } else {
    var scheme =  objApp.and;
  }
  if (objApp.appName == "Facebook") {
    return true ;  // FB doesn't need app, can be logged in via browser
  } else {
    return appAvailability.check(
      scheme,
      function() {  // success callback
        console.log(scheme + " is Installed") ;
        return true ;
      }, function () {  // Error callback
        console.log(scheme + " is NOT Installed") ;
        alert("You do not have the " +objApp.appName+ " app installed.") ;
        return false ;
      }
    );
  }

  checkSocialApp(appObj).then(function(response){ // errors on 'then'
    if (response == true) {  // app IS installed
      console.log("CheckApp True") ;
      appObj.loginApp() ;
    } else if (response == false) {  // app IS NOT installed
      console.log("CheckApp False") ;
      appObj.disableApp() ;
    }
  }) ;

The above errors out on the .then.  So I try to bind it to a promise.resolve:
var promise1 = Promise.resolve(checkSocialApp(appObj)) ;
promise1.then(function(response) {
   if (response == true) ...

This executes the checkSocialApp function successfully (as I see proper console messages printing from within that function), but I am not getting the response back into the remaining part of the .then for processing.

A:

You have to do something like this return a promise in your function:
function checkSocialApp(objApp) {
   return new Promise( function(resolve)={
         if (device.platform == "iOS") {
               var scheme =  objApp.ios;
           } else {
               var scheme =  objApp.and;
          }

          if (objApp.appName == "Facebook") {
                   resolve (true) ;  // FB doesn't need app, can be logged in via browser
          } else {
                 return appAvailability.check(
                       scheme,
                           function() {  // success callback
                                 console.log(scheme + " is Installed") ;
                                 resolve (true) ;
                            }, function () {  // Error callback
                                     console.log(scheme + " is NOT Installed") ;
                                     alert("You do not have the " +objApp.appName+ " app installed.") ;
                                  resolve (false) ;
                           }
                    );
        }
   })
}

  checkSocialApp(appObj).then(function(response){ // errors on 'then'
    if (response == true) {  // app IS installed
      console.log("CheckApp True") ;
      appObj.loginApp() ;
    } else if (response == false) {  // app IS NOT installed
      console.log("CheckApp False") ;
      appObj.disableApp() ;
    }
  }) ;

