Angular Http Priority

2019-05-27 09:36发布

I am making lots of API calls in my applications i.e say 50.

The total time for completing all the api calls will be around 1 minute. The priority for all the api calls will 2. I have enabled the angular cache.

So in meantime if the user of my applications just want to focus on the some of the api calls among the all i.e say just 6 api calls.

Then once again I will project that 6 api calls with priority 1 .

But still I dont get what I aimed ? i.e these 6 api calls need to receive the data asap.

Kindly refer the below example code .

On Initial load :

for(var i=1,priority=19;i<=19,priority>=1;i++,priority--)  
{
$http.get("http://localhost:65291/WebService1.asmx/HelloWorld"+i+"?test=hari",{priority:2})
.then(function(response) { });
}
}

On some event click :

$http.get("http://localhost:65291/WebService1.asmx/HelloWorld7?test=hari",{priority:1})
.then(function(response) { });
}

1条回答
Anthone
2楼-- · 2019-05-27 09:52

if you want send multiple http request one shot then use $q.all

Inside the loop push the http requests to an array and send that http array at once.

var httpArr = []

for (var i = 1, priority = 19; i <= 19, priority >= 1; i++, priority--) {
    httpArr.push($http.get("http://localhost:65291/WebService1.asmx/HelloWorld" + i + "?test=hari", {
        priority: 2
    }))
}
$q.all(httpArr).then(function(response) {
    console.log(response[0].data) //1st request response
    console.log(response[1].data) //2nd  request response
    console.log(response[2].data) //3rd request response
})
查看更多
登录 后发表回答