Will ajax callback be executed after the loop?

I have this Javascript / JQuery code:

$.ajax({...}).done(function() {
    console.log('done');
});
for(var i = 0; i < 10; i++)
    console.log(i);

Say the ajax request ends when the loop has reached the 5th iteration, does the browser pause the loop and start the callback, or will it wait for the loop to complete?

To make it more understandable, is such a conclusion possible?
0
1
2
3
4
done
5
6
7
8
9

or will always be displayed:
0
1
2
3
4
5
6
7
8
9
done

, , , , .

+4
2

, , , ajax.

EDIT: , , , - ***** DONE! ***** 2828725

var promise = new $.Deferred(),
    i = 1, j = 0, to = 5000000;

promise.done(function() {
    document.write("***** DONE! *****\n" + i);
    i = to;
});

setTimeout(function() {
    promise.resolve();
}, 1000);

for(i = 1; i < to; ++i) {
    console.log(++j);
    //alert(++j);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
Hide result
+1

, , , ,

fiddle

time = new Date()
new Request.JSONP({
    url: 'http://jsfiddle.net/echo/jsonp/',
    onSuccess: function (response) {

        console.log('done ' + (new Date()-time))
    }
}).send();

for (var i = 0; i <= 65535; i++) {
    for (var j = 0; j <= 10000; j++) {
            if (i===65535 && j == 10000) 
            {
        console.log('loop complete ' + (new Date()-time))
            }
    }
}
0

Source: https://habr.com/ru/post/1570479/


All Articles