As in the title, I'm wondering how to send few asynchronous requests and, after they're processed, change a part of my website (in this instance a single counter). Doing it synchronous way gives just the outcome I expect to get, but it freezes the website for a quite a long time. My current code is as follows:
var twitchReq = new XMLHttpRequest;
function twitchR(x,y)
{
var api_key = "some_api_key";
var source = "https://api.twitch.tv/kraken/streams/" + y + "?&client_id=" + api_key + "&callback=";
x.open("GET", source, false);
x.send(null);
var z = twitchPR();
return z;
}
function twitchPR()
{
if(twitchReq.status == 200)
{
if(twitchReq.readyState == 4)
{
var a = twitchReq.responseText;
var b = JSON.parse(a);
var c = twitchD(b);
return c;
}
}
}
function twitchD(x)
{
if(x.stream == null)
{
console.log("Offline");
return 1;
}
else
{
console.log(x);
return 2;
}
}
function twitchWidget()
{
var t = [
"Nervarien",
"Jankos",
"LolVander",
"RossBoomsocks",
"ESL_LOL",
"Xayoo_",
"Kubon_"
]
var j=0;
var n=0;
for(i=0;i<7;i++)
{
if(twitchR(twitchReq,t[i]) == 1)
{
j=j+1;
}
else
{
n=n+1;
}
}
document.getElementById("test-1").innerHTML = "Online: " + n + "<br>" + "Offline: " + j;
}
I've figured out that the nail in the coffin of this solution is waiting for one request to be finished before sending another, not to mention freezing any other code in queue. So: how do i process the outcome of my request once they are finished and wait for the others to be finished at the same time?
EDIT: Regarding 'possible duplicate' mark - I've seen that thread and yet, I couldn't tell how to solve my problem. The difference is, he could just use ajax promises, while I had no idea how to make one, and therefore couldn't use solutions posted there.