0
votes

I have a function that needs to test an array of parameters for errors before proceeding. Depending on the item, the test may or may not involve a call to the server.

I've implemented an array of $q's in order to ensure they all finish before evaluating the test results. I'm returning the array using $q.all.

I know that all promises are resolving, because I can step through each one and see the resolution, but for some reason, the resolution is not reaching the topmost .then.

Topmost .then:

$scope.BigTest().then(function(result){
 //examine the array of results & then call the function we want to execute
 // we never ever reach here
},
function(error){
  // handle the error
  // we never ever reach here either
});

the function using $q,all():

$scope.BigTest = function(){
    var promises = new Array();
    for (var x = 0; x < $scope.testingStuff.length; x ++){
        var temp = $q.defer();
        if ($scope.testingStuff[x].localTestingGoodEnough){
            if (test){
                temp.resolve(true);
            }
            else{
                temp.resolve(false);
            }
        }
        else{
            var getServerStuff = ServerService.testServer($scope.testingStuff[x]);
            getServerStuff.then(function(result){
                // I've debugged through here and know this is successfully happening        whenever necessary, and that the value is appropriate
                temp.resolve(result.value);
            },function(error){
                temp.resolve(false);
            });
        }
        promises[x] = temp.promise;
    }
    return $q.all(promises);
} 

As noted in the psuedocode, the problem is that the entire array of promises is never being resolved when the test requires a call to the server.

In the cases where there is no server call required, the set resolves as expected.

Any ideas as to why this is not resolving? Perhaps I'm not using $q.all() properly?

1
try temp.resolve(result.data.value);...assuming your service is using $http - charlietfl

1 Answers

0
votes

It turns out that I WAS in fact doing it correctly, but I had a typo in my code where everything in the "BigTest" else statement was surrounded by brackets:"()". Although this threw no errors, it was blocking the resolution of the server calls. Removing the brackets solved the issue.