0

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
    })
  }
Santhosh Kumar
  • 381
  • 1
  • 5
  • 13
  • 3
    `How to make asynchronous promises synchronous?` you can't make synchronous from asynchronous - a moments thought you'd realise why - asynchronous implies that something will (or may not) happen after an unknown amount of time - how can that be converted to a "known" amount of time? – Jaromanda X May 17 '17 at 02:08
  • 3
    You can't make async to sync. But you can do operation as like it were sync. From your sample above, simply chain the promises. – choz May 17 '17 at 02:09
  • 1
    @choz, the main issue I believe is the `return` at the end - you can't "chain" that in a promise chain :p unless you return the promise, then the caller to `this.getUserInfo` must use `.then` (or await if this.getUserInfo was tagged async) - asynchronous code infects up the chain of calling functions :p or use callbacks as you suggested below – Jaromanda X May 17 '17 at 02:10
  • 1
    @JaromandaX Ackk true, didnt notice that.. Well, Simply dont `return`. OP can consider to use callback function instead. – choz May 17 '17 at 02:11
  • 2
    and one more point ... "asyncrhonous promises" is redundant, as almost everything about promises is asynchronous by design - by using promises, you've sold your soul to the asynchronous lord of the underworld :p – Jaromanda X May 17 '17 at 02:13
  • Thank you folks. All I did was remove the "return apkPkg" and make var apkName, appVersion, appBuild, appID; global outside the function and the whole thing works like a charm. I will come up with a robust solution later but for now I am back in game. – Santhosh Kumar May 17 '17 at 05:08
  • 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 }) } – Santhosh Kumar May 17 '17 at 16:55

0 Answers0