3 ms·
Yes, it's similar to a game loop, but in Node it's called an event loop. All JavaScript code executes in the main thread, with the event loop (libev) coordinati
by tlrobinson 16y ago
Yes, it's similar to a game loop, but in Node it's called an event loop. All JavaScript code executes in the main thread, with the event loop (libev) coordinating asynchronous IO APIs, the thread pool, and signal handlers. Node continues executing until there are no more pending operations, then exits.
Most IO in Node doesn't require additional threads; it uses libeio for asynchronous IO on POSIX systems (I believe they're currently working on an abstraction for asynchronous IO in POSIX and Windows).
It does need to use the thread pool for IO operations that only have a blocking API (stat() and friends, legacy database libraries, etc). The blocking API call is executed in a separate thread so the main thread is never blocked on IO.
This is a bit of a (necessary) hack, and ideally all IO could be done using non-blocking APIs.