Javascript order of execution - How come it does this?
<p>I am quite on lost on why Javascript does certain things in this way, or maybe I coded something wrong. But how come the code after $.getJSON gets executed before the callback is complete?</p>

<pre><code>window.ratingCallback = function(data){

     if(data[0].code == 3){  
         ratingHTML = buildHTML(0);
         console.log('a'+ratingHTML);
         return;
     }

     ratingHTML = buildHTML(data[0].rating);
     console.log('b'+ratingHTML);
     return;
 };

  function buildHTML(pageRating){

     for(counter = 1; counter &lt;= 5; counter++){

      if(counter == pageRating){
              ratingHTML += '&lt;input name="star1" type="radio" class="star" value="'+counter+'" checked="checked"/&gt;';
      } else {
              ratingHTML += '&lt;input name="star1" type="radio" class="star" value="'+counter+'"/&gt;';    
      }
     }

     return ratingHTML;
 }


$.getJSON("https://domain.com/"+data+"?jsoncallback=?");

console.log(ratingHTML+"WHY DOES THIS EXECUTE FIRST AND NOT AFTER THE CALLBACK SINCE IT IS PLACED AFTER getJSON????");
</code></pre>

<p>The reason I want this to be different is that I need ratingHTML to be a global variable.</p>

<p>I understand that the callback gets called when the remote server has given a response, but can I let the rest of the script wait with further execution? Without placing all code in the callback function or any function?</p>

<p>Thank you very much!</p>

<p>Ice </p>
