延迟和承诺angularjs不能正常工作

时间:2015-01-27 09:44:59

标签: javascript jquery angularjs

我现在在angularjs中遇到defer promise的问题。

这是我的代码:

var defer = $q.defer();

    defer.promise
    //First Execution
    .then(function () {
        TestService.GetSchoolYear().then(function (results) {
            $scope.schoolYears = results.data;
            $scope.schoolYear = results.data[0].schlYearName;
        });
    })
    //Second Execution
    .then(function () {
        TestService.GetAffiliation().then(function (results) {
            $scope.affiliation = results.data;
            $scope.school = results.data[0].affiliation;
        });
    })
    //Third Execution
    .then(function () {
        TestService.GetDepartment().then(function (results) {
            $scope.departments = results.data;
            $scope.department = results.data[0].depName;
            $scope.depCode = results.data[0].depCode;
        });
    })
    //Fourth Execution
    .then(function () {
        TestService.GetYearLevel2($scope.depCode).then(function (results) {
            $scope.yearLevels2 = results.data;
            $scope.yearLevel2 = results.data[0].yearName;
        });
    });
    defer.resolve();

所有执行都是有序的。但延迟执行不同的功能不按顺序。

我的代码出了什么问题?它有什么问题。

我在这里得到了这个参考: https://thinkster.io/egghead/promises/

我是否遗漏了我的代码。我想要的只是按顺序执行函数,因为其他函数需要一些数据。

谢谢。

3 个答案:

答案 0 :(得分:1)

由于TestService的各种功能似乎都回复了承诺,因此您甚至不需要$q.defer - 实际上这将是deferred anti-pattern

将它们链接在一起并return结果:

return TestService.GetSchoolYear()
    .then(function (results) {
       $scope.schoolYears = results.data;
       $scope.schoolYear = results.data[0].schlYearName;

       return TestService.GetAffiliation();
    })
    .then(function (results) {
       $scope.affiliation = results.data;
       $scope.school = results.data[0].affiliation;

       return TestService.GetDepartment();
    })
    .then(function (results) {
       $scope.departments = results.data;
       $scope.department = results.data[0].depName;
       $scope.depCode = results.data[0].depCode;

       return TestService.GetYearLevel2($scope.depCode);
    })
    .then(function (results) {
       $scope.yearLevels2 = results.data;
       $scope.yearLevel2 = results.data[0].yearName;
       return results;
    });

答案 1 :(得分:0)

您的then处理程序中没有返回任何内容,因此then()方法创建的承诺会立即解决并立即转移到下一个then()

您需要返回一个承诺,以便有时间等待:

.then(function () {
    // v--------  here
    return TestService.GetSchoolYear().then(function (results) {
        $scope.schoolYears = results.data;
        $scope.schoolYear = results.data[0].schlYearName;
    });
})

还应该注意,此处不需要$q.defer()使用$q.defer()几乎没有理由。

您可以使用$q.when()

 $q.when()
 .then(function () {
     // ....
 })

或者更好的是,只需通过您正在进行的第一次异步调用启动您的承诺链:

TestService.GetSchoolYear().then(function (results) {
    $scope.schoolYears = results.data;
    $scope.schoolYear = results.data[0].schlYearName;
})
.then(function () {
    return TestService.GetAffiliation().then(function (results) {
        // ...
    });
})  // ...

答案 2 :(得分:0)

对于链接工作的承诺,then函数应该返回一些东西。

试试这个:

defer.promise
    //First Execution
    .then(function () {
        return TestService.GetSchoolYear().then(function (results) {
                 $scope.schoolYears = results.data;
                 $scope.schoolYear = results.data[0].schlYearName;
                 return results;
        });
    })
    //Second Execution
    .then(function () {
        return TestService.GetAffiliation().then(function (results) {
            $scope.affiliation = results.data;
            $scope.school = results.data[0].affiliation;
            return results;
        });
    })
    //Third Execution
    .then(function () {
        return TestService.GetDepartment().then(function (results) {
                 $scope.departments = results.data;
                 $scope.department = results.data[0].depName;
                 $scope.depCode = results.data[0].depCode;
                 return result;
        });
    })
    //Fourth Execution
    .then(function () {
        return TestService.GetYearLevel2($scope.depCode).then(function (results) {
            $scope.yearLevels2 = results.data;
            $scope.yearLevel2 = results.data[0].yearName;
            return results;
        });
    });
    defer.resolve();