如何在$ http链中处理回调地狱?

时间:2015-03-10 05:29:26

标签: javascript angularjs

我有很多$http个请求如下:

        Scholarship.loadMaxAcademicYear().success(function (AcademicYearId) {
            if (AcademicYearId) {
                Scholarship.deleteAllCurrentData(data).success(function () {
                    Scholarship.copyDataToCurrentYear().success(function(){
                        Scholarship.getPromises(null, AcademicYearId).then(function () {
                            $scope.isSuccess = true;
                            $timeout(function () {
                                $scope.isSuccess = false;
                                $scope.isSubmit = false;
                                $scope.confirmModal.close();
                            }, 5000);
                        }, function(err){
                            importError();
                        });
                    }).error(function(){
                        importError();
                    })
                }).error(function(){
                    importError();
                });
            }
        }).error(function (err) {
            importError();                
        });

我想将错误回调减少到最后一个,如下所示:

        Scholarship.loadMaxAcademicYear().success(function (AcademicYearId) {
            if (AcademicYearId) {
                Scholarship.deleteAllCurrentData(data).success(function () {
                    Scholarship.copyDataToCurrentYear().success(function(){
                        Scholarship.getPromises(null, AcademicYearId).then(function () {
                            $scope.isSuccess = true;
                            $timeout(function () {
                                $scope.isSuccess = false;
                                $scope.isSubmit = false;
                                $scope.confirmModal.close();
                            }, 5000);
                        }
                    })
                })
            }
        }).error(function (err) {
            importError();                
        });

我正在考虑使用async,但Angular可能有办法处理这类问题。是否有可能在AngularJs中这样做?

1 个答案:

答案 0 :(得分:1)

即使在你的第二个例子中,你仍然有一个厄运的金字塔。这里的关键是使用.then()方法来允许承诺链接:

Scholarship.loadMaxAcademicYear().then(function(response) {
  var academicYearId = response.data;
  if (academicYearId) {
    return Scholarship.deleteAllCurrentData(academicYearId)
    .then(function() {
      return Scholarship.copyDataToCurrentYear();
    }).then(function() {
      return Scholarship.getPromises(null, academicYearId);
    }).then(function() {
      $scope.isSuccess = true;
      return $timeout(function() {
        $scope.isSuccess = false;
        $scope.isSubmit = false;
        $scope.confirmModal.close();
      }, 5000);
    });
  }
}).catch(function(err) {
  importError();
});

我们也可以使用.bind()

来缩短这一点
Scholarship.loadMaxAcademicYear().then(function(response) {
  var academicYearId = response.data;
  if (academicYearId) {
    return Scholarship.deleteAllCurrentData(academicYearId)
    .then(Scholarship.copyDataToCurrentYear.bind(Scholarship))
    .then(Scholarship.getPromises.bind(Scholarship, null, academicYearId))
    .then(function() {
      $scope.isSuccess = true;
      return $timeout(function() {
        $scope.isSuccess = false;
        $scope.isSubmit = false;
        $scope.confirmModal.close();
      }, 5000);
    });
  }
}).catch(importError);