Ajax and javascript don’t use threads
Since ajax, a lot of people are thinking that asynchrone means “in a separated thread”. They are wrong!
Synchronous
The XMLHttpRequest object gives you the option to make a synchronous request to a server with the parameter async set to false. It means that when you call the server, all javascript executions will stop and wait for the server to respond to the request. I never saw it used. In fact it is more confusing that anything else because the page seems frozen and users don’t like frozen things. If you use it, you are not cool!
Asynchronous
Now you are being cool! The asynchronous is used by everyone and it doesn’t freeze the page while the call to the server is done. So, if you have a call that takes 30 seconds before receiving the answer, your users will never know that you’re a bad server-side coder that do not optimize the code he’s writing (I’m just joking).
The misconception of asynchronous
People take for granted that because it’s asynchronous, it’s a thread. They are partially right. There must be a thread created by the browser to keep the javascript running while it makes a request to the server. It’s internal and you don’t have access to that thread. But, the callback function called when the server responds to the ajax request is not in a thread.
I’ll explain clearer. If javascript runs some code that takes 5 seconds to execute and an ajax response arrives at 2 seconds, it will take 3 seconds before it will be executed (before the callback function is called). That’s because javascript itself doesn’t create a thread to execute the ajax response from the server and simply waits that all executions are terminated before starting a new one.
So if you’re running a lot of ajax requests simultaneously, you might get some weird behavior because they will all wait one on another before executing themselves.
#UPDATE 2007-06-12
Following one of my readers comment (BK), I have written a sequel to this article : Ajax, javascript and threads : the final truth.


RSS without the echo chamber
I agree with you on the fact that the main thread will be used BUT, from my experience, I’m not quite sure on the order of the requests… Maybe it depends on the browser used but using IE, I often got functions interupted by the new incoming response.
Briefly, say I have CallPage1, CallPage2 and DoSomething. Both CallPage are async.
CallPage1 called
CallPage2 called
DoSomething called
CallPage1 Returned (DoSomething is now paused)
DoSomething ended
CallPage2 Returned
This is important to know if you use any shared variable because you can’t rely on the fact that it won’t change during the process of your sub. If it’s not clear, tell me.
This is an important point - not guarantee-able of course, given that it’s totally down to browser implementation, but I’d hate to meet the browser that spawned a new thread for each AJAX request!
It should also be remembered that whilst you need to bear in mind the volatility of shared variables, the ordering of AJAX responses should *never* be relied on or even thought about - that’s what asynchronous means!
To prevent the alert, confirm, etc… from pausing the script execution, you could overwrite the default function with some custom dialogs, so you never get the default dialogs halting your script. This way you can guarantee that your callbacks will execute just after your current running code
[…] Simard has been writing about threading and JavaScript and came up with his Final […]
Those are thread communication stuffs.
Request thread send a message to main thread and ask main thread to fire the onComplete event.
[…] Ajax and javascript don’t use threads | Javascript Kata: according to this i’m not cool, i want to be cool, but again a decision i didn’t have input on and don’t have enough time to put up a fight. […]
[…] 从Dan Simardçš„ä¸¤Â ç¯‡æ–‡ç« ä»¥åŠè¯»è€…的一些评论ä¸ï¼Œæˆ‘们看到在一个执行时间很长的javascript函数(比如longtimeF())ä¸çš„æŸä¸ªæ—¶é—´ç‚¹æ‰§è¡Œä¸€ä¸ªajax的请求。å¯ä»¥è‚¯å®šçš„æ˜¯browser肯定会å¯åŠ¨ä¸€ä¸ªthreadsæ¥æ‰§è¡Œè¿™ä¸ªXHR的异æ¥è¯·æ±‚,这个threadséšç€response的到æ¥è€Œç»“æŸï¼Œè€Œcallback函数并ä¸åœ¨è¿™ä¸ªçº¿ç¨‹å†…执行。 […]
[…] no es asÃ. Lo cierto, tal y como hemos podido leer en algún que otro artÃculo es como si la “máquina” o hilo de ejecución que interpreta el javascript se […]