0

And just when I think I have my head wrapped around promises I try to implement something and stumble in such a way that makes me realize I don't fully understand Angular yet.

Arrgh!

I am initializing my app and creating an Init Controller and Service factory to ask the user to set permissions the app will need. However, in the .factory I can't figure out how to create the sequence of promises correctly. I want to sequentially ask the user and thus am creating a promise chain.

Promise 1 = ask for sms access
   process 1 results
   Promise 2 = ask for GET_ACCOUNTS
      process 2 results
      Promise 3 = ask for GPS
         process 3 results
         return to controller with all results

The first Promise is throwing me, because the plugin I am using returns an async on the requesting user permission:

function requestReadPermission() {  // from cordova-plugin-sim
// no callbacks required as this opens a popup which returns async
  window.plugins.sim.requestReadPermission();
}

return $q(requestReadPermission)
.then(function(status) {
   console.log(status) ; // just see what comes back 
   // do something with status
   function getAccounts() {
      cordova.plugins.diagnostic.requestContactsAuthorization() ;
   }
   return $q(getAccounts)
}).then(function(status) {
   console.log(status) ; // just see what comes back 
   // do something with status
   function getGPS() {
      cordova.plugins.diagnostic.requestLocationAuthorization() ;
   }
   return $q(getGPS)
}).then(function(status) {
   console.log(status) ; // just see what comes back 
   // do something with status
   return allResults ;
})

Additionally, the second two promises already look to be callback functions...so I don't know if I can implement them the way I want. They come from cordova-plugin-diagnostics and in the documentation are called the following way:

cordova.plugins.diagnostic.requestContactsAuthorization(function(status){
    if(status === cordova.plugins.diagnostic.permissionStatus.GRANTED){
        console.log("Contacts use is authorized");
    }
}, function(error){
    console.error(error);
});

cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
    switch(status){
        case cordova.plugins.diagnostic.permissionStatus.NOT_REQUESTED:
            console.log("Permission not requested");
            break;
        case cordova.plugins.diagnostic.permissionStatus.GRANTED:
            console.log("Permission granted");
            break;
        case cordova.plugins.diagnostic.permissionStatus.DENIED:
            console.log("Permission denied");
            break;
        case cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
            console.log("Permission permanently denied");
            break;
    }
}, function(error){
    console.error(error);
});
1

The diagnostic plugin functions must be passed a callback function to receive the async result from the native part of the plugin. However, you can wrap this in a way to make it Angular-friendly by returning a promise which is resolved by the outcome of the two async callbacks. For example:

function myFunction(){
    var q = $q.defer();
    var statuses = {};
    cordova.plugins.diagnostic.requestContactsAuthorization(function(status){
        console.log('contacts: '+status);
        statuses['contacts'] = status;
        cordova.plugins.diagnostic.requestLocationAuthorization(function(status){
            console.log('location: '+status);
            statuses['location'] = status;
            q.resolve(statuses);
        });
    });
    return q.promise;
}

You can then call your wrapper function using the Angular-style promise:

myFunction().then(function(statuses){
    for(var permission in statuses){
        console.log(permission+' is '+statuses[permission]);
    }
});
|improve this answer|||||
  • ok, I knew the callbacks were throwing me off. How would you incorporate the 'sim' permissions too? I was looking for a cordova-plugins-diagnostic that would allow for me to access the sim card but haven't found one - thus I was using the cordova-plugin-sim to get sim info - but the sim's request permission implementation is an async popup with no call back on it. btw - thanks for your help. I was going in the right direction but was def way off track. – rolinger Oct 23 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.