10

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!

0

3 Answers 3

6

It is synchronous. You can tell if you set a breakpoint after the loop and one inside the loop. The breakpoint in the loop will be hit before the one after the loop.

This is assuming the breakpoints will be hit in the execution, object/array being looped over has items, etc.

5

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]
1
  • So you guys are saying the iteration will not continue until the function called is done running, correct?
    – adi518
    Jul 24, 2015 at 0:06
1

It's synchronous. You can see the source code here, there's no a synchronous programming involved.

http://www.james.padolsey.com/jquery/#v=1.11.2&fn=jQuery.each

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.