如何实现http获取请求的承诺?

问题描述:

我有以下的HTTP GET请求: -如何实现http获取请求的承诺?

$http({ 
    method: 'GET', 
    url: 'getData.php', 
    params: {bill: 'Active'} 
}) 
.then(function (response) { 
    bill=response.data.results; 
}); 

的请求进行多次,我想对于处理要等到所有的http请求完成。为此,我想实现承诺,然后在函数结束时返回承诺。我怎样才能实现这个HTTP获取请求的承诺?

+0

请参阅[AngularJS $ q服务API参考 - $ q.all](https://docs.angularjs.org/api/ng/service/$q#all)。 – georgeawg

var arr=[]; 
arr.push(callHttp()); 
arr.push(callHttp()); 
arr.push(callHttp()); 

promise.all(arr).then(....) 


function callHttp(){ 
return $http({ 
    method: 'GET', 
    url: 'getData.php', 
    params: {bill: 'Active'} 
}) 
} 

您的请求(.then(...)之前的所有内容)已经一个承诺。

将您的请求数组放入Promise.all以获得一旦完成所有事情就完成的新承诺。