等待多个 $resource 执行

Waiting multiple $resource to execute

当'getUser'函数执行时不等待解析来自('get'函数)两个资源的所有数据并且$scope.requests变量没有所有数据

     $scope.getUser = function (applicantUser) {
        get(applicantUser).then(function (result) {
            $scope.requests = checkDuplicateInObject('applicationId', result);
            $scope.displayHistory = true;
        }, function (error) {
        });
    };


    var checkDuplicateInObject = function (propertyName, inputArray) {
        var temp = [], result = [];
        for (var i in inputArray) {
            if (temp.indexOf(inputArray[i][propertyName]) != -1) {
                result.splice(temp.indexOf(inputArray[i][propertyName]), 1);
                result.push(inputArray[i]);
            } else {
                temp.push(inputArray[i][propertyName]);
                result.push(inputArray[i]);
            }
        }
        return result;
    };

    var get = function (applicantUser) {
        var defer = $q.defer();
        var u = {};
        var temp = [];
        var promise1, promise2;
        $scope.requests = [];
        if (applicantUser !== undefined && applicantUser.length >= 2) {
            if (!isNaN(applicantUser) && angular.isNumber(+applicantUser)) {
                u.userSerialNumber = applicantUser;
            } else {
                u.userLogonName = applicantUser;
            }

            ListUserInfoSN.post(u).$promise.then(function (result) {
                    result.forEach(function (user) {
                        promise1 = GetListOfRequestsPerUser.get({
                            userId: '', newUserSerialNumber: user.userSerialNumber
                        }).$promise;
                        promise2 = GetListOfRequestsPerUser.get({userId: user.userId, newUserSerialNumber: ''})
                            .$promise;
                    }, function () {
                    });
                    $q.all([promise1, promise2]).then(function (result) {
                        result[0].data.forEach(function (request) {
                            request.request.forEach(function (r) {
                                if (r[2] == r[3]) {
                                    r[5] = 'success';
                                } else {
                                    r[5] = 'warning';
                                }
                                if (r[4] == true) {
                                    r[5] = 'danger';
                                }
                            });
                            temp.push(request);
                        });
                        result[1].data.forEach(function (request) {
                            request.request.forEach(function (r) {
                                if (r[2] == r[3]) {
                                    r[5] = 'success';
                                } else {
                                    r[5] = 'warning';
                                }
                                if (r[4] == true) {
                                    r[5] = 'danger';
                                }
                            });
                            temp.push(request);
                        });
                        defer.resolve(temp);
                    });
                }, function () {
                    promise1 = GetListOfRequestsPerUser.get({userId: '', newUserSerialNumber: applicantUser}).$promise;
                    promise2 = GetRequestsById.get({requestId: applicantUser});
                    $q.all([promise1, promise2]).then(function (result) {
                        result[0].data.forEach(function (request) {
                            request.request.forEach(function (r) {
                                if (r[2] == r[3]) {
                                    r[5] = 'success';
                                } else {
                                    r[5] = 'warning';
                                }
                                if (r[4] == true) {
                                    r[5] = 'danger';
                                }
                            });
                            temp.push(request);
                        });
                        result[1].data.forEach(function (request) {
                            request.request.forEach(function (r) {
                                if (r[2] == r[3]) {
                                    r[5] = 'success';
                                } else {
                                    r[5] = 'warning';
                                }
                                if (r[4] == true) {
                                    r[5] = 'danger';
                                }
                            });
                            temp.push(request);
                        });
                        defer.resolve(temp);
                    });
                }
            );
        } else {
            u = {};
            temp = [];
            $scope.displayHistory = false;
        }
        return defer.promise;
    };

我更新了代码,但我有同样的错误

使用$q.all()然后解决承诺。

$q.all() 允许您等待一系列承诺,$q.all() 会将这些组合成一个承诺。

您的代码已更新:

$scope.getUser = function (applicantUser) {
  get(applicantUser).then(function (result) {
      $scope.requests = result;
  }, function (error){
      console.log(error);
  });
};

var get = function (applicantUser) {
  var defer = $q.defer();
  var temp = [];

  var promise1 = GetListOfRequestsPerUser.get({userId: '', newUserSerialNumber: applicantUser}).$promise;

  var promise2 = GetRequestsById.get({requestId: applicantUser}).$promise;

  $q.all([promise1, promise2]).then(function(result) {

    // GetListOfRequestsPerUser
    result[0].data.forEach(function (request) {
      temp.push(request);
    });

    // GetRequestsById
    result[1].data.forEach(function (request) {
      temp.push(request);
    });

    defer.resolve(temp);
  });

  return defer.promise;
};

一切都很简单,你不需要$q.defer:

var promise1 = smth1.then(...);
var promise2 = smth2.then(...);

return $q.all([promise1, promise2]).then(function (promisesResultArray) {
   // promisesResultArray[0] is result of first promise
   // promisesResultArray[1] is result of second promise
   return whateverYouwant; //or simply promisesResultArray
})

P.S.: 在这里添加 - 如果一些承诺失败,$q.all 失败并且不等待其他承诺。 (而且没有什么比 $q.allSettled)