I'm from a C background and find the asynchronicity of javascript very cool. I don't know however how things are asynchronous. Is it that every function-call is practically a new thread?
Asked
Active
Viewed 571 times
0
-
`That's how I've been programming in it`. Could you elaborate? – Joren Nov 20 '13 at 01:17
-
[You can find a very good insight here](http://rickgaribay.net/archive/2012/01/28/node-is-not-single-threaded.aspx). – moonwave99 Nov 20 '13 at 01:17
-
You will probably find [this answer](http://stackoverflow.com/questions/7575589/how-does-javascript-handle-ajax-responses-in-the-background/7575649#7575649) helpful about threading, event queue and asynchronous ajax. – jfriend00 Nov 20 '13 at 01:32
1 Answers
5
No, it's not a new thread: it's running an event loop.
Examples of systems in C that work the same way:
- select-based polling where you stay on one thread, handle the result of select, then call select again to get the next thing to work on
- Classic Win32 programming, where you send messages to the event queue. The core of the program is "Dequeue message. Dispatch message. Repeat until quit message received"
- Just about every other GUI programming environment ever built :-)
While you could think of it as a thread for a first approximation, it isn't really. Threads run in parallel, events are run serially. You never have to worry about concurrent access to data, but you do have to worry about starving the event loop (not returning to it often enough).

Chris Tavares
- 29,165
- 4
- 46
- 63