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