jquerysynchronizationeach

Function call inside a $.each loop, is it async or sync?


Assume a simple for-each loop ($.each), where for-each iteration I call a function set in the global scope. Does the function run synchronously or asynchronously? Assume no Ajax, so what the function does is completely synchronous, albeit merely executed. In other words, I wonder whether the function call itself is blocking within the iteration or not.

Thanks!


Solution

  • Everything in JavaScript is synchronous. If you're not using timeouts or callbacks everything will be "synchronous".

    Simple example should prove it.

    var data = [1,2,3];
    var results = [];
    $.each(data, function(d) { results.push(d); });
    
    console.log(results); // [1,2,3]