I am simply trying to loop through ajax calls.
The problem is that I am losing flow control because of functions and therefore becoming completely confused.
The first examples that I saw were using XMLHTTPRequest.onreadystatechange.
Now I am using .onload
Both seem to be completing the loop, and then calling the function once.
I tried to just avoid the function completely with a while loop that checks the ready state, but that didn't seem to get me anywhere. -- Multiple while loops with functions to wait 1 second and then console.log.... this is waaay too complex, and my readyState just gets stuck at 1???...
...which leads me to...
...The other issue is .send()
.send() is always the last thing... but according to https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState
4 means:
"DONE The fetch operation is complete. This could mean that either the data transfer has been completed successfully or failed."
So why do the if statements check that the status is 4 and then do .send() Wouldn't you have to send the GET request before having a readystate of 4?
Here is the base - without any of my attempts
var tickersArray = ['AA', 'AAPL', 'AMZN'];
var responsesArray = [];
for (i = 0; i < tickersArray.length; i++) {
console.log(tickersArray[i]);
var actualURL = "https://api.intrinio.com/companies?identifier=" + tickersArray[i];
var xhr = new XMLHttpRequest();
xhr.open("GET", actualURL, true);
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", 'Basic ' + btoa(myuser + ':' + mypswd));
/*
This is where I am having trouble, I want to just stop the loop
until i get the response, so i can parse the json and append it to my responses array
*/
};