Reputation:
I want to know how the engine in node.js know when to call and execute the queued operation. I understand that node.js is single threaded and uses asynchronous non-blocking to execute operations.
But let's say you are calling something from the database and node.js queues the operation and doesn't wait for this to execute further lines of code. Being single thread it stores the data in channels(if I am not wrong). But how does the server know that the network isn't busy and it is the right time to execute the queued operation.
Or it executes the queued operation at the end of the program. This can't be the case. So I am curious to know what happens under the hood and how does the node server know when to execute the queued operation.
Upvotes: 0
Views: 69
Reputation: 29916
Node internally uses the underlying operating systems non-blocking io API-s. See overlapped io
, WSAEventSelect
for Windows, select
for Linux.
Upvotes: 0
Reputation: 707326
It's not clear what you mean by "channels" as that is not a built-in node.js concept or a term that is used internally to describe how node.js works. The tutorial you reference in your comment is talking about the Java language, not the Javascript language so it has absolutely nothing to do with node.js. The concept of "channels" in that tutorial is something that that specific NIO library implements in Java.
But, if you're really just asking how async operations work in node.js, I can explain that generaly concept.
node.js works off an event queue. The node.js interpreter grabs the next event in the event queue and executes it (typically that involves calling a callback function). That callback function runs (single threaded) until it returns. When it returns, the node.js interpreter then looks in the event queue for the next event. If there are any, it grabs that next event and calls the callback associated with it. If there are not events currently in the event queue, then it waits for an event to be put in the event queue (with nothing to do expect perhaps runs some garbage collection).
Now, when you runs some sort of asynchronous operation in your Javascript (such as a DB query), you can your db query function, that initiates the query (by sending the query off to the database) and then immediately returns. This allows your piece of Javascript that started that query to then return and give control back to node.js.
Meanwhile, some native code is managing the connection to your database. When the response comes back from the database, an event is added to the internal node.js event queue.
Whenever the node.js interpreter has finished running other Javascript, it looks in the event queue and pulls out the next event and calls the callback associated with that. In that way, your DB query result gets processed by your code. In all cases here, an asynchronous operation has some sort of callback associated with it that the interpreter can call when it finds the event in the event queue.
Being single thread it stores the data in channels(if I am not wrong).
node.js doesn't have a "channels" concept so I'm not sure what you meant by that.
Or it executes the queued operation at the end of the program.
When the result comes back from the database, some native code managing that will insert an event into the node.js event queue. The Javascript interpreter will service that event, then next time it gets back to the event loop (other Javascript has finished running and it's time to check for the next event to process).
It's important to understand that initiating an asynchronous operation is non-blocking so you get this:
console.log("1");
callSomeAsyncFunction(someData, (err, data) => {
// this callback gets called later when the async operation finishes
// and node.js gets a chance to process the event that was put
// in the event queue by the code that managed the async operation
console.log("2");
});
// right here there is nothing else to do so the interpreter returns back
// to the system, allowing it to process other events
console.log("3");
Because of the non-blocking nature of things, this will log:
1
3
2
Some other references on the topic:
How does JavaScript handle AJAX responses in the background?
Where is the node.js event queue?
Why does a while loop block the node event loop?
Upvotes: 1