When I call this functions I get apkPkg a bunch of undefines. When I put BP's I do break into apkName, appVersion, appBuild and appID but since all these are promises apkPkg has already been executed and will have bunch of undefines. Question1: How to sync this async operation so the apkPkg has good values. Q2: Can I use the commented code so to make this compact and git rid of the extra variable? Please advice, thanks
this.getUserInfo = function () {
var apkName, appVersion, appBuild, appID;
$cordovaAppVersion.getAppName().then(function (name) {
apkName = name;
});
$cordovaAppVersion.getVersionNumber().then(function (version) {
appVersion = version;
});
$cordovaAppVersion.getVersionCode().then(function (build) {
appBuild = build;
});
$cordovaAppVersion.getPackageName().then(function (ID) {
appID = ID;
});
/*
$cordovaAppVersion.getAppName().then(function (apkName) {});
$cordovaAppVersion.getVersionNumber().then(function (appVersion) {});
$cordovaAppVersion.getPackageName().then(function (appID) {});
$cordovaAppVersion.getVersionCode().then(function (appBuild) {});
*/
var apkPkg = apkName + " Version " + appVersion + " Build: " + appBuild + " ID: " + appID;
return apkPkg;
};
Sorry I tried to put this in the comment and goofed up. So I am putting this here This is a logical approach. $q was new to me and my understanding is $q will sync up the async calls. This is a very generic example people can use in other cases too.:
this.getUserInfo = function () {
return $q.all([
$cordovaAppVersion.getAppName(),
$cordovaAppVersion.getVersionNumber(),
$cordovaAppVersion.getVersionCode(),
$cordovaAppVersion.getPackageName()
]).then(function (appInfo) {
var apkPkg = appInfo[0] + ' Version ' + appInfo[1] + ' Build: ' + appInfo[2] +
' ID: ' + appInfo[3]
return apkPkg
})
}