visit
“Dude, just stop, it’s not your level.”Well, after a continuous three 40 min sessions of Pomodoro (by the way it really works and I will write about that shortly) and 3 cups of black coffee, I think I finally got this.
A callback as written in the Node.js docs, “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.” What this means is, whenever we write an asynchronous code you can just use the callback function saying you it’ll call when it’s finished as you move on to the next thing. This is what basically a non-blocking method is. Node.js handles the I/O operations in a non-blocking manner which contains the callback functions so that nothing will be blocked and we can execute multiple shits simultaneously.
So, now as we know how Node.js handles asynchronous code, let’s get to the real thing, the infamous event loop. An event loop is a thing that handles and processes the events and converts them into the callbacks. So, whenever a network I/O call comes, Node sends that to the event loop where it gets into the task queue and starts executing as the Node returns back to the runtime environment and moves on to the next.Now coming back to the first subheading, Node is not single-threaded. Mm… Whenever we write an asynchronous code Node takes the user script onto the single thread and the fs I/O operations are handled by the and OS i.e, Libuv has something called thread pool where each of the threads are assigned to a task from the task queue by the event loop and they start executing simultaneously. So, the whole architecture of Node.js makes it simple to have high-level parallelism.Hence, this is what makes Node.js great, having asynchronous code support and running it in a single thread (though not exactly). Well, I guess this covers it. I hope this makes sense if not, let me know - as I really want to know more.