Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Race conditions with JavaScript event handling?

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:

  1. We invoke function foo when a mobile page is loaded.

  2. 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.

  3. 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:

  1. 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?

  2. What if the tap occurs first? do_tap is guaranteed to complete before foo starts, right?

like image 843
Crashalot Avatar asked Dec 23 '11 00:12

Crashalot


People also ask

Can you have a race condition in JavaScript?

Yes, we can have race conditions in Node. js!

What are examples of race conditions?

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.

What is race condition?

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.


2 Answers

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.

like image 144
jfriend00 Avatar answered Oct 31 '22 20:10

jfriend00


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.)

like image 34
Pointy Avatar answered Oct 31 '22 19:10

Pointy