Ответ 1
Да, это очень элегантно обрабатывается AngularJS, поскольку его служба $http
построена вокруг PromiseAPI. В принципе, призывы к методам $http
возвращают обещание, и вы можете легко сцедить promises с помощью метода then
. Вот пример:
$http.get('http://host.com/first')
.then(function(result){
//post-process results and return
return myPostProcess1(result.data);
})
.then(function(resultOfPostProcessing){
return $http.get('http://host.com/second');
})
.then(function(result){
//post-process results of the second call and return
return myPostProcess2(result.data);
})
.then(function(result){
//do something where the last call finished
});
Вы также можете комбинировать пост-обработку и следующую функцию $http
, все зависит от того, кто заинтересован в результатах.
$http.get('http://host.com/first')
.then(function(result){
//post-process results and return promise from the next call
myPostProcess1(result.data);
return $http.get('http://host.com/second');
})
.then(function(secondCallResult){
//do something where the second (and the last) call finished
});