0

So i have this code

    getTens.getToken($rootScope.webUserInfo[0].username).then(function(resulttoken) {
        $rootScope.userInfo = resulttoken;
        $sessionStorage.token = resulttoken[0].token;
        $sessionStorage.userInfoStorage = $rootScope.userInfo;
        CasesGroupByCaseStatus.getListing($rootScope.webUserInfo[0].username).then(function(data){
          $rootScope.listingDetails = data;
          $sessionStorage.listingDetailsStorage = $rootScope.listingDetails;
        });

        CasesGroupByCaseStatus.caseStatusCount($rootScope.webUserInfo[0].username).then(function(resultcaseStatus){
          $rootScope.dashboardStatus = resultcaseStatus;
          $sessionStorage.dashboardStatusStorage = $rootScope.dashboardStatus;
          console.log("it is finished")
        });
      });
      return [200, { authorizationToken: $sessionStorage.token}];

In my code, i want it to complete the function first before returning the value but what happens is that it fires the return first wtout running the function, How do i handle this?

This code is my app.js. So i cant do stuffs such as scope.watch and all.

Kingsley Simon
  • 2,090
  • 5
  • 38
  • 84
  • instead of return the actual object, why don't you return a promise and resolve it after all other promises are resolved? – sdfacre Mar 25 '16 at 03:10

1 Answers1

0

I'm not sure if this is a good way to solve the problem, but for my application I put a

$rootScope.$broadcast('loaded', true);

inside the asynchronous part's callback/success function and then put my code inside this:

$scope.$on('loaded', function(){ //everything is loaded });

Like I said, not sure if this is a good solution or not. I used this to make my application wait for a asynchronous function to finish reading a file before starting my application. Some answer is better than no answer I guess.

Kyle
  • 403
  • 2
  • 10