$q.all() 同步执行而不是异步执行

$q.all() performing synchronously not asynchronously

我正在开发一个应用程序来从我的 API 服务器检索数据,将其与新数据进行比较,然后将新数据(如果不存在)推送到 API 服务器.目前我正在研究使用 $http、promises 和 $q.all() 检索服务器上现有数据的功能。当我将所有承诺推送到一个数组然后使用 $q.all() 时,它会同步触发并且不会等待承诺解决。

我已经完成了,据我所知,在我想要 运行 的所有操作中都加入了 return 异步。我也阅读了很多有类似问题的帖子,并进行了这些调整但没有成功。

这是来自 fileData 服务的相关代码:

var apiPromise = function (url) {
    var deferred = $q.defer();
    $http.get(url)
        .then(function (result) {
            deferred.resolve(result.data);
            return;
        });
    return deferred.promise;
};

this.GetExistingStudentTest=function(studentid){
    return apiPromise('http://localhost:65196/api/PS/GetStudentTests?studentid=' + studentid + '&testid=2')
};

this.GetExistingStudentTestScores = function (testid) {
    return apiPromise('http://localhost:65196/api/PS/GetStudentTestScore?testid=' + testid)
};

正在使用的控制器代码是:

$scope.previewUpload = function () {
    var promises = [];
    for (var i = 0; i < $scope.students.count; i++) {
        var student = $scope.students.students[i];
        promises.push(fileData.GetExistingStudentTest($scope.students.students[i].studentId)
            .then(function (data) {
                for (var j = 0; j < data.length; j++) {
                    var prevTest = {
                        testId: data[j].id,
                        studentId: student.studentId,
                        rawDate: data[j].test_date.substr(5, 2)+data[j].test_date.substr(2,2),
                        date: data[j].test_date,
                        testGradeLevel: data[j].grade_level,
                        scaleScores: {}
                    };
                    promises.push(fileData.GetExistingStudentTestScores(prevTest.testId)
                        .then(function (scoreData) {
                            console.log(scoreData);
                            for (var k = 0; k < scoreData.length; k++) {
                                switch (scoreData[k].testscoreid) {
                                    case "1":
                                        prevTest.scaleScores.english = scoreData[k].numscore;
                                        break;
                                    case "2":
                                        prevTest.scaleScores.math = scoreData[k].numscore;
                                        break;
                                    case "3":
                                        prevTest.scaleScores.reading = scoreData[k].numscore;
                                        break;
                                    case "4":
                                        prevTest.scaleScores.science = scoreData[k].numscore;
                                        break;
                                    case "5":
                                        prevTest.scaleScores.writing = scoreData[k].numscore;
                                        break;
                                    case "6":
                                        prevTest.scaleScores.composite = scoreData[k].numscore;
                                        break;
                                    case "451":
                                        prevTest.scaleScores.ELA = scoreData[k].numscore;
                                        break;
                                    case "452":
                                        prevTest.scaleScores.STEM = scoreData[k].numscore;
                                        break;
                                }
                            }
                            $scope.tests.previous.tests.push(prevTest);
                            $scope.tests.previous.count++;
                            return scoreData;
                        })
                    );
                }
                return data;
            })
            );

    }



    $q.all(promises).then(function () {
        console.log('Completed Test Retrieval');
        for (i = 0; i < $scope.tests.refined.count; i++) {
            console.log($scope.tests.refined.tests[i]);
            console.log($scope.tests.previous)
            for (j = 0; j < $scope.tests.previous.count; j++) {
                console.log($scope.tests.previous.tests[j]);
                if ($scope.tests.previous[j].studentId === $scope.tests.refined[i].studentId && $scope.tests.previous[j].rawDate === $scope.tests.refined[i].rawDate) {
                    console.log('Match');
                }
            }
        }

    });

    $scope.validateTests = true;
};  

我需要看到的是测试分数 return,测试被推送到正确的数组,然后 $q.all() 解析以允许新数据和现有数据进行比较。实际发生的是当内部承诺被解析时,$q.all() 解析并且嵌套的 for 循环没有 运行 因为数组中没有值。

首先,我会从删除 defer 开始,我认为您不需要它。 $http returns 一个承诺,所以你可以做到这一点。请阅读这篇文章,它改变了游戏规则! https://www.codelord.net/2015/09/24/%24q-dot-defer-youre-doing-it-wrong/

var apiPromise = function (url) {
    return $http.get(url).then(function (result) {
       return result.data;
    });
};

其次,您不应该在 $q 之外对可能尚未解析的数据执行代码。您要么需要嵌套该逻辑,要么编写一个函数

// get all tests
var tests = students.map(function(s){
    return fileData.GetExistingStudentTest(s.studentId)
});

// wait for tests to resolve
$q.all(tests).then(function(resolvedTests){
   var transformedTests = // transform logic
   // get all test scores
   var testScores = transformedTests.map(function(t){
       return fileData.GetExistingStudentTestScores(t.testId);
   });
   // wait for test scores to resolve
   $q.all(testScores).then(function(resolvedTestScores){
      // you now have all the tests and test scores resolved...
      processTests(transformedTests,resolvedTestScores);
   });

});