I am currently trying to search a bit about how Javascript handles data race in parallel or asynchronous calls. As far as I'm concerned, the only way to make asynchronous stuff on my code is by AJAX calls (correct if I'm wrong!), but I suppose that if there is another way, it will be handled likewise.
So, as an example, suppose that I have this AJAX call:
jQuery.ajax({
//stuff
success: function (data) {
//do some fancy things with some DOM elements
},
//possibly more stuff
});
Suppose now that outside that call I am running other things, and I end up doing something with the same DOM element that my success callback deals within my AJAX call (i.e. appending elements on a table at the same time that my success callback tries to do the same thing). On another words, my configuration ends in some sort of a data race.
How does Javascript deals with this? When my AJAX response comes back, will my current running function be preempted so that the AJAX call be executed? Is there something that guarantees me that what I'm doing with my DOM element (or a javascript variable) is some sort of an atomic operation? So in case I need to run a function that must be atomic, how can I tell that to the interpreter?
Also, is there some shielding on Javascript to prevent deadlocks? Say, if I run two parallel AJAX calls and each one depends on the finishing of the other.