0

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
         */

        };
ThomasRones
  • 657
  • 8
  • 29
  • 1
    https://stackoverflow.com/questions/5485495/how-can-i-take-advantage-of-callback-functions-for-asynchronous-xmlhttprequest – luxdvie Nov 10 '17 at 14:18
  • 1
    Well sounds like you do not want a for loop, but you want a queue. – epascarello Nov 10 '17 at 14:24
  • 1
    you can't "loop" through ajax calls with any degree of control because they're executed asynchronously. There's no way to stop the loop while a request is in progress because that request is on another thread and is out of your control. If you want to wait until the previous request has completed before you trigger the next one, simply queue them up, maintain a pointer to the next request in the array and then execute it from the onreadystatechange callback of the previous request. – ADyson Nov 10 '17 at 14:25
  • ok that makes sense. I just misunderstood the meaning of asynchronous, I thought it meant that this call isn't in sync with the call to refresh the page because this call is to another server and no call is sent to the server that is hosting my content. – ThomasRones Nov 10 '17 at 14:38
  • Arrghhh... Still having trouble with the logic. I'm tempted to just make it synchronous because the application is really only for my use, it just gets data. – ThomasRones Nov 10 '17 at 16:47
  • 1
    why not post your updated code and the problem you're having. It's probably not very far from working properly, and it's a very useful concept to learn. – ADyson Nov 10 '17 at 21:14

0 Answers0