如何实现http get请求的承诺?

时间:2016-11-05 22:53:13

标签: javascript angularjs

我有以下http获取请求: -

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

请求多次,我希望处理等待所有http请求完成。为此,我想实现promises,然后在函数结束时返回promise。如何围绕此http get请求实现promise?

2 个答案:

答案 0 :(得分:0)

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'}
})
}

答案 1 :(得分:0)

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

将您的请求数组放在Promise.all中,以便在一切都完成后获得新的承诺。