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 use setTimeout
to invoke foo
again (with one second delay) if a counter is greater than 0
. If the counter hits 0
, 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 queue do_tap
to start executing after foo
finishes? In other words, are we guaranteed that once foo
starts, we can never see execution of foo
and do_tap
interleaved?
What if the tap occurs first? do_tap
is guaranteed to complete before foo
starts, right?
Yes, we can have race conditions in Node. js!
A simple example of a race condition is a light switch. In some homes, there are multiple light switches connected to a common ceiling light. When these types of circuits are used, the switch position becomes irrelevant. If the light is on, moving either switch from its current position turns the light off.
A race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable.
Except 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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With