Grokking the Event Loop and Event Queue in Node.js - Non-blocking programming:
Non-blocking programming - which in JavaScript has 3 components.
Source for gaining a deeper understanding of how Node.js works and why Node.js is essential to certain file system operations:
Pluralsight:: Introduction to Node.js Events and Streams
Here is a code example:
Line 1. I’m importing HTTP using the commonJS style, which Node.js uses.
Line 2. Instantiate server from HTTP.
Line 4. Registers the‘ request’ event to execute an anonymous callback to return a response of HTTP 200 and a text message of “Hello, this is a dog.” NOTE: the callback takes two parameters, the request object coming in and the response object that will get assembled in the callback’s code with the appropriate data and RETURNED to the caller. In this case, the caller is likely a human using the web.
Line 10. Registers the ‘request’ event again to do something else; in this case, log something.
Line 11. Registers the close event; when the close event gets put into the event queue, the V8 js engine will pick up the next event (FiFo) “no event will be executed before its time” - and matches the event to the events registered in this case, close and will execute the function callback which in this case is to log something.
NOTE: I made the distinction of explaining that the importing of the HTTP module is using the commonJS importing style. Because JS uses 2 importing styles that depend on what toolset you are using, you will be using. The commonJS is as seen on line one using the require function.
The other method is when a module of code requires importing using the ES6 way of doing it.
Which looks like this:
Source: