Is there a delay between task completion and callback function execution?

I am learning Node.js and some javascript. I read up some stuff of thinks like queues and execution stacks.

I am trying to calculate time taken by a websocket request to complete. A very typical emit is of form:

microtime1  = getTimeNow;

socket.emit("message","data", function (error, data) {
    // calculate time taken by using microtime module by getting updated time and calculating difference.
       microtime2  = getTimeNow;
       time = microtime2 - microtime1;

})

If I am sending multiple messages, can I rely on callback getting executed without delay or can there be a hold up in the queue and callback won’t get executed.

In other words, would callback only get called once it’s in stack or does it get executed while it’s waiting to be picked up in the queue ?

Hope, I was able to explain my question.

Answer

In other words, would callback only get called once it’s in stack or does it get executed while it’s waiting to be picked up in the queue ?

The callback gets executed after the event, that it is waiting for, is resolved.

So the callback should work just fine, however there is a caveat. because node-js is single threaded, you could have another process that’s blocked the main thread.

For example the simple view of execution may look like this. One event is processed, and then another one is processed after.
enter image description here

However, in reality it may look more like this

The single thread is meant for the main thread only, things like the IO operations are done on another dedicated thread that will notify main thread when it’s done, and then the callback can be executed after

enter image description here

The problem occurs if your main thread becomes busy while waiting for the network action to complete enter image description here

This is hard to predict though and depends on what the rest of the app is doing. If your app is not doing anything else, this likely won’t be an issue. But, IMHO, a better way is to make hundreds or thousands of calls and allow get an average which will account for other possible causes for discrepancies in the delta.


Additional data from c-sharpcorner.com

enter image description here The above diagram shows the execution process of Node.js. Let’s understand it step by step.

Step 1

Whenever a request comes to Node.js API, that incoming request is added to the event queue. This is because Node.js can’t handle multiple requests simultaneously. The first time, the incoming request is added to the event queue.

Step 2

Now, you can see in the diagram that one loop is there which always checks if any event or request is available in event queue or not. If any requests are there, then according to the “First Come, First Served” property of queue, the requests will be served.

Step 3

This Node.js event loop is single threaded and performs non blocking i/o tasks, so it sends requests to C++ internal thread pool where lots of threads can be run. This C++ internal thread pool is the part of event loop developed in Libuv. This can handle multiple requests. Now, event loop checks again and again if any event is there in the Event Queue. If there is any, then it serves to the thread pool if the blocking process is there.

Step 4

Now, the internal thread pool handles a lot of requests, like database request, file request, and many more.

Step 5

Whenever any thread completes that task, the callback function calls and sends the response back to the event loop.

Step 6

Now, event loop sends back the response to the client whose request is completed.