0

Following example construct:

function b(){
  $('#a').on('blur', function(){ 
    console.log('blur');
  });
}
function c(){
  $('#a').trigger('blur');

  //pseudo: e.g. reading value from #a
}
b();
c();

Now my Question is, will a call to trigger execute the callback synchronously and then jump back to function "c" stack ? Or is it possible that some code after "trigger" in stack c could be executed before the callback (async operation) ?

rgruenke
  • 85
  • 1
  • 8
  • 1
    I'm not exactly sure what you're asking, but all event handlers are synchronous from the point they are fired, as you can see here: http://jsfiddle.net/5qczrvc5/. None of the callbacks in your example are async. – Rory McCrossan Nov 18 '15 at 14:35
  • Possible duplicate of: http://stackoverflow.com/questions/2035645/when-is-javascript-synchronous – Brain Foo Long Nov 18 '15 at 14:39
  • okay so after I call the trigger function all handlers get called synchronously, and 'block' the remaining instructions of the c function stack until all handlers returned? – rgruenke Nov 18 '15 at 14:40

1 Answers1

1

Javascript is single-threaded, which means that your code executes in order – never mind that the call is passed to an event handler.

The handler and its invocation are not what makes execution async. It's the events themselves which are, because they are usually subject to unpredictable, external factors like user interaction or network latency. Here, the event is triggered predictably, synchronously, so the order of execution is in fact set in stone.

It would be a different story with multi threading, e.g. if web workers were involved. But that's not the case.

hashchange
  • 7,029
  • 1
  • 45
  • 41
  • okay thanks, I knew that's it single threaded, but as I remember are the stack instructions interchangable so that some intstructions can come in while some stack of a function is not done yet (the event loop and it's task queue where you can place callbacks ). As far as I know are these operations async (one could place callbacks there with setTimeout for example). Am I wrong in some kind? – rgruenke Nov 18 '15 at 14:54
  • Seems like one can't create any concurrency (as described) by himself. – rgruenke Nov 18 '15 at 15:28
  • Ok, I can see the source of the confusion now :) There is no magic stack reordering in the JS runtime – otherwise it would cease to be a stack. You seem to suspect that the `trigger` call itself makes things async, as if it would execute like a `setTimeout()` call. Then, the callback (event handler) would end up in the task queue, which is processed once the stack has been cleared. As a result, the callback would execute last. And that would always be the case, not randomly. – But that is not what happens, which drives home the point that `trigger` is processed as part of the stack. – hashchange Nov 18 '15 at 15:39
  • Incidentally, there is an excellent overview of stack, task queue and event loop in this talk: http://goo.gl/kCJLw1 Maybe you'll like it - I did :) – hashchange Nov 18 '15 at 15:39
  • Exactly yes, that's also what I investigated. I already saw this video from jsconf before, they were the root cause of why I wanted to be exactly sure ;) thanks – rgruenke Nov 18 '15 at 15:43
  • 1
    It's funny because the JS Browser "runtime" itself has "concurrent" functions (like the rendering -> same Thread), and with setTimeout you can set your callbacks into the queue which are then, by nature, non blocking for those "concurrent" functions. So only "concurrent" as you wish ;) – rgruenke Nov 18 '15 at 15:47