0
我有需要继续之前测试的参数错误的数组的函数解析。根据项目的不同,测试可能涉及或不涉及到服务器的呼叫。
我已经实现了一个$ 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,所有():
$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);
}
如在伪码所指出的,问题是,承诺的整个阵列永不当测试需要调用服务器时解决。
在不需要服务器调用的情况下,该集合按预期方式解决。
任何想法为什么这不解决?也许我没有正确使用$ q.all()?
尝试'temp.resolve(result.data.value);'...假设你的服务使用'$ http' – charlietfl