3
kpenc
7y

Node.js runs on a single thread. Why everybody says it's asynchronous?

Comments
  • 0
    Isn't it the same as plain js where everything runs on the same thread, except ui and requests?
  • 0
    As far as I understood, all I/O actions like reading files, saving etc are asynchronous and don't effect the main thread. And cause I/O is typically really slow, nodejs is relatively fast.
  • 3
    When asynchronous methods are called they are not executed instantly but added to the event queue, this means that the server can still continue handling requests. The content of the function still runs synchronously but will only execute when the server isn't busy with request handling. The actions performed should be relatively small, and are chained together (use callbacks or promises) so that each one can happen quickly without blocking the thread.

    https://nodejs.org/en/about/
  • 0
    @zantekk There is nothing really asynchronous, it only looks like that. Everything event, function call, etc. is first added to a queue and executed when the time comes. It just happens fast most of the time.
  • 0
    Is your CPU core multitasking? Not really, but it sure feels like it does stuff simultaneously :) (no HT)
  • 0
    Main event loop is single threaded. IO like disk access etc is separate threads. As long as you don't do anything serious, it will work... somehow. Otherwise, use a language with proper threading and spin up as many event loops as you want. Lot more efficient than having a shitton of node processes with their half arsed ipc.
Add Comment