0

I have read bunch of posts on call backs and asynchronous Ajax nature, however most of them use jQuery and its kind of confusing.

function nextMonth(){
 for(var i loop){
   for(var p loop){

       //once proper i and p found

        var output = getData(date);
        var result = JSON.parse(output);

        //do something with that data
   }
  }
}
function getData(date){
   var xmlhttp;
   xmlhttp=new XMLHttpRequest();
   xmlhttp.onreadystatechange=function(){
      if (xmlhttp.readyState==4 && xmlhttp.status==200){
         var str = xmlhttp.responseText;
         return str;
      }
   }
   xmlhttp.open("POST","url",true);
   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
   xmlhttp.send(encodeURI("date"+date));
}

Loops make it difficult for me to see it, any way with the suggestions on the call back to still keep it functional?

"Ry" marked it a duplicate and linked to the jQuery post, i clearly stated the difference of two questions.

Liox
  • 19
  • 2
  • Promises. Built into modern browsers now as [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), with polyfills available for older ones. – Ry- Jul 02 '18 at 21:01
  • As i mentioned in my title, NO jQuery, the post you referred to is jQuery question. Mine is Javascript. Not the same – Liox Jul 02 '18 at 21:05
  • @Liox: When you read the other question/answer you will see that jQuery is used just an example and that the solutions are library agnostic. – Felix Kling Jul 02 '18 at 21:15
  • @FelixKling, i understand that. However my question really is around the looping, and i just can't see how that answer applies in this case. I would appreciate at least a hint around it... Ive spent hours trying to apply it to my case. – Liox Jul 02 '18 at 21:24
  • If you use `async/await` then you don't have anything special to do wrt to loops, you can keep using `for...`. If you are using promises without `await` then you can use `Promise.all` to wait for multiple promises to be resolved. In the later case it helps to not think about loops but about how to perform the following conversion `array of data => filtered array of data => array of promises`. Since you are not providing a complete example (i.e. we don't know what the loops are for or what `date` is and where it comes from) it's hard to provide a concrete solution. – Felix Kling Jul 02 '18 at 21:26

0 Answers0