0

I'm a beginner in Node right now. I understand that callbacks prevent Node from blocking while waiting for I/O or some other process, but what is Node doing while waiting for that I/O to finish? One tutorial I read on nodejitsu says, " A callback is a function called at the completion of a given task; this prevents any blocking, and allows other code to be run in the meantime." but what other code is it running? Let's say the other code a given block is dependent on the information being retrieved from the database. Can Node go handle other connections while waiting for the callback or can it only run other code in the current block?

user137717
  • 2,005
  • 4
  • 25
  • 48

2 Answers2

2

Node works off an event queue. When you start an async operation, that operation proceeds independently from node in the background. Node continues running the rest of your code in the current execution thread until it gets to the end of that execution thread (e.g. everything returns back up to the top of the stack). While node is running a current execution thread, no other threads of execution will run in node (ignoring for the moment generators and fibers). This is why people refer to it as single threaded. It runs a particular thread of execution one at a time sequentially, not multiple threads of execution in parallel. One must finish before the next one runs.

When one thread of execution finishes, if there are other events in the event queue, then node will pop the next event off the queue and run it. Other events in the event queue can be from anything. They can be from timers, from the async event you just start or from async events elsewhere in your program (e.g. other database operations finishing in your specific question). If there are no other events (so thus no code ready to be run), then node just waits until the next event occurs (doing nothing except perhaps garbage collection).

Then, when the async operation completes (in the background), it will add an event to the event queue to call it's callback. If nothing else is currently running in node at that time, then that async callback event will run. If something else is currently running in node, then that operation will finish executing and when it does, the next event in the event queue will run and so on...

In this way, there is only ever one thread of execution at a time in nodejs. When one finishes, the nodejs execution engine gets the next event off the event queue and runs it. When async operations finish and want to call their callback, they insert an event into the event queue.

This answer which describes the event queue and contains some other references was written for a browser, but pretty much all the same rules apply in nodejs so it might also help you understand more about this.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    this is a key concept in node that everyone must wrap their heads around: no other queued events will run until the currently running function returns all the way back up its call stack, reaches the end of the script and exits. Only then will the next event be dequeued and run. (fibers and generators not included) – Andras Dec 03 '14 at 19:16
  • @jfriend00 the way you talk about single threaded makes it seem like a normal synchronous program. If Node is waiting on database I/O will it pop other ready tasks off the event queue or is it stuck in the currently executing block? Let's say I have a route to / and it accesses the file system as the first move. Can Node get out of that block and handle other connections while waiting for the file system to finish retrieving the file? – user137717 Dec 03 '14 at 20:39
  • @user137717 - A single thread of execution runs until completion before any other code can run. If your node server is serving a request and then it makes a database call with a callback set up and then returns back to the system, then node is completely free to do anything else (including serve other incoming requests). Sometime in the future, when the database call completes, node will call the callback and your request will finish it's work. – jfriend00 Dec 03 '14 at 21:04
  • @user137717 - You appear to be confused about something you call "currently executing block". Follow the path of execution. When it returns back to the system, then other events can run even if there are still callbacks queued up to run later. Javascript has no notion of "currently executing block". It just has a thread of execution that either has more lines of code to execute or has returned back to the system so the system can run other events (which lead to other threads of execution). – jfriend00 Dec 03 '14 at 21:05
0

It's doing nothing while waiting. But it might wait for multiple IO actions to finish, and then will invoke the callback for the one that finishes first. So if you are waiting for multiple connections, node will do something everytime a connection opens. It will even do that if some code triggered by the first connection has registered a callback to wait for some file IO in the meantime.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375