We understand that JavaScript is single threaded, but we want to confirm our understanding of asynchronous event handling in JavaScript. More importantly, we want to confirm we're not exposed to potential race conditions.
Conceptually, our mobile app works like this:
We invoke function
foo
when a mobile page is loaded.At the end of
foo
, we usesetTimeout
to invokefoo
again (with one second delay) if a counter is greater than0
. If the counter hits0
, we load a new page. The timeout is saved in a variable.If a button is tapped, we invoke function
do_tap
and clear the timeout variable saved in step two (and do other stuff).
do_tap
and foo
both update the same page element, and we want to confirm that they wouldn't step on each other.
Questions:
Assume a tap occurs during the execution of
foo
. Will the browser queuedo_tap
to start executing afterfoo
finishes? In other words, are we guaranteed that oncefoo
starts, we can never see execution offoo
anddo_tap
interleaved?What if the tap occurs first?
do_tap
is guaranteed to complete beforefoo
starts, right?
We understand that JavaScript is single threaded, but we want to confirm our understanding of asynchronous event handling in JavaScript. More importantly, we want to confirm we're not exposed to potential race conditions.
Conceptually, our mobile app works like this:
We invoke function
foo
when a mobile page is loaded.At the end of
foo
, we usesetTimeout
to invokefoo
again (with one second delay) if a counter is greater than0
. If the counter hits0
, we load a new page. The timeout is saved in a variable.If a button is tapped, we invoke function
do_tap
and clear the timeout variable saved in step two (and do other stuff).
do_tap
and foo
both update the same page element, and we want to confirm that they wouldn't step on each other.
Questions:
Assume a tap occurs during the execution of
foo
. Will the browser queuedo_tap
to start executing afterfoo
finishes? In other words, are we guaranteed that oncefoo
starts, we can never see execution offoo
anddo_tap
interleaved?What if the tap occurs first?
do_tap
is guaranteed to complete beforefoo
starts, right?
4 Answers
Reset to default 10Except for web workers and cooperating frames or windows (which aren't being used here), Javascript is single threaded within a given window so there are never two threads of execution running at the same time in that window. As such, you don't ever have to worry about race conditions that might be a typical worry when using threads.
Under the covers, Javascript has an event queue. Your current thread of execution will run to completion and then when it completes, the javascript interpreter will check the event queue to see if there are more things to do. If so, it fires that event and starts up another thread of execution. Pretty much everything goes through that event queue (timers, key events, resize events, mouse events, etc...).
You can read more about it and see a bunch of relevant references in one of my other answers on this subject.
Event execution proceeds single-threaded until the event has been handled. Until that time, no other event loop will be initiated.
In other words, while a handler is running for some event, no other handler for any other event will interrupt it.
Thus the answer to both question 1 and question 2 is "Yes." (This is, of course, barring browser bugs, but if you take that into account you can't get very far. It's not like there are any synchronization primitives to fall back on. I say this only because there was a period of time during which Safari could fire a "DOMready" event in the middle of running another "DOMready" event handler. That was, however, quite clearly a bug.)
As long as the first thing Do_tap() does is clearTimeout there will be no chance for foo to be running during the execution of Do_tap(). However, if an asynchronous process was initiated in foo() such a database request then there could be a potential wait to access the database in Do_tap() as foo() finishes its request, and if foo() has a callback function that could theoretically be called after Do_tap() was done executing.
I didn't catch anyone else saying this, but I think the answer to #2 is that different browser implementations can and do differ in subtle ways as to which queued events are processed first. No, there is no possibility of interleaving, but whether setTimeout(func, 0) or a mouse event is handled first is not guaranteed by the language spec and in practice can matter. Whereas setTimeout(func, 100) is guaranteed to be processed later than pending events received during the currently handled event.
Just saying.
foo
to start executing beforeDo_tap
returned (and vice versa). Read this article and you'll understand how the execution queue in the browsers works: ejohn.org/blog/how-javascript-timers-work – Šime Vidas Commented Dec 23, 2011 at 0:35