AngularJs $ q.all没有使用混合的嵌套承诺来解决

时间:2013-10-29 17:57:02

标签: javascript angularjs

我有一个函数需要在继续之前测试参数数组的错误。根据项目,测试可能涉及也可能不涉及对服务器的调用。

我已经实现了一个$ q的数组,以确保它们在评估测试结果之前完成。我正在使用$ q.all返回数组。

我知道所有的承诺都在解决,因为我可以逐步完成并看到分辨率,但由于某种原因,分辨率并没有达到最高分。然后。

最顶层。然后:

$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
});

使用$ 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);
} 

正如伪代码中所提到的,问题是当测试需要调用服务器时,永远不会解析整个promises数组。

在不需要服务器调用的情况下,该集合按预期结算。

关于为什么这不解决的任何想法?也许我没有正确使用$ q.all()?

1 个答案:

答案 0 :(得分:0)

事实证明我实际上正确地做了,但我的代码中有一个拼写错误,其中“BigTest”else语句中的所有内容都被括号括起来:“()”。虽然这没有任何错误,但它阻止了服务器调用的解析。删除括号解决了这个问题。

相关问题