执行“ 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;
};
我更新了代码,但有相同的错误
答案 0 :(得分:1)
使用$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;
};
答案 1 :(得分:1)
一切都很简单,您不需要$ 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这样的东西)