내 $scope
에 항목 배열이 있습니다. 각 항목에 대해 세 가지 $ http 요청을 실행해야합니다. 이러한 요청은 실패하지 않았는지 여부에 관계없이 특정 순서로 실행되어야합니다. 나는 약속의 패러다임을 가지고 이것을 우아하게하는 방법을 모르겠습니다. 중복 코드가 많아서 혼란스러워 보입니다. 나는이 잘못을해야만한다. 현재 다음과 같은 내용을 가지고 있습니다.
$scope.items = getItems();
$scope.currentIndex = 0;
$scope.executeItem = function() {
$http.get($scope.items[$scope.currentIndex].urlA).then(
function (resA) {
$scope.items[$scope.currentIndex].urlAWorks = true;
$http.get($scope.items[$scope.currentIndex].urlB).then(
function (resB) {
$scope.items[$scope.currentIndex].urlBWorks = true;
$http.get($scope.items[$scope.currentIndex].urlC).then(
function (resC) {
$scope.items[$scope.currentIndex].urlCWorks = true;
$scope.currentIndex = $scope.currentIndex + 1;
$scope.executeItem();
},
function (errC) {
$scope.items[$scope.currentIndex].urlCWorks = false;
$scope.currentIndex = $scope.currentIndex + 1;
$scope.executeItem();
}
)
},
function (errB) {
$scope.items[$scope.currentIndex].urlBWorks = false;
}
);
},
function (errA) {
$scope.items[$scope.currentIndex].urlAWorks = false;
$http.get($scope.items[$scope.currentIndex].urlB).then(
function (resB) {
$scope.items[$scope.currentIndex].urlBWorks = true;
$http.get($scope.items[$scope.currentIndex].urlC).then(
function (resC) {
$scope.items[$scope.currentIndex].urlCWorks = true;
$scope.currentIndex = $scope.currentIndex + 1;
$scope.executeItem();
},
function (errC) {
$scope.items[$scope.currentIndex].urlCWorks = false;
$scope.currentIndex = $scope.currentIndex + 1;
$scope.executeItem();
}
)
},
function (errB) {
$scope.items[$scope.currentIndex].urlBWorks = false;
}
);
}
);
};
정말 정확하게 약속하고 있습니까? 이것은 꺼져 보인다.
고맙습니다.
데이터를 순서대로 * 요청 *해야합니까? 아니면 순서대로 처리해야합니까? – Blazemonger
@Blazemonger가 순서대로 데이터를 요청합니다. – user70192
단일 요청에서 모든 데이터를 가져 오는 방법을 찾은 다음 살펴볼 수 있습니다. – Blazemonger