Hello. There is such a JS code:

$(document).ready(function() { $.getJSON('https://...?callback=?', function(data) { //ответ от сервера успешно получаю alert("Im in JSON"); }); alert("Im out JSON"); }); 

Question: why as a result is the “Im out JSON” alert first executed, and only then “Im in JSON”? Why is this kind of logic "the other way around"?

Reported as a duplicate at Grundy. javascript 7 Sep '17 at 15:17 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • four
    Asynchrony - Igor
  • Lightning fast reply, thanks! - 200OK

2 answers 2

$.getJSON an asynchronous event, that is, it is executed after receiving a response from the server and does not block the execution of the rest of the code.

    This is due to asynchrony. To disable it, you can add this:

     jQuery.ajaxSetup({async:false}); 

    UPD: But better not.