If I am performing 2 ajax requests with callback functions at the same
time, can I be guaranteed that the callbacks will not be executed at
the same time, or is there a chance that the statements could be
interleaved?
As has been pointed out, the order in which the responses will be
received cannot be guaranteed (hence the term "asynchronous").
However, the callback function that processes the first-received-
response will run to completion before the second-received-response's
callback function is executed, due to the single-threaded nature of
JavaScript implementations.
However, another possible complication arises if either or both of
your callback functions are doing something that uses setInterval or
setTimeout, such as animating the display of updated content received
in the response. As the triggering of the timer functions is
independent of the main execution context from which they are
initiated, it would be possible to end up with some such sequence of
events as:
Make request A;
Make request B;
....
Response to request B received: setInterval i started;
....
setInterval i does something;
....
setInterval i does something;
....
Response to request A received; setInterval ii started;
....
setInterval ii does something;
....
setInterval i does something;
....
setInterval i does something;
....
setInterval ii does something;
and so on. So although JavaScript isn't threaded, in that each of the
individual "chunks" of execution delimited by "..." above will run to
completion before any other of the chunks is allowed to commence, it
is still possible for separate components of your overall application
to have their execution interleaved in an unpredictable manner.
Regards,
Nick.
(omitting .sig as apparently Google Groups mucks it up ;-)