Grokking the Event Loop and Event Queue in Node.js - Non-blocking programming:

Non-blocking programming - which in JavaScript has 3 components.

  1. the registering of events and their respective callback:: this is done just by a standard programming declaration of the event and their respective event handler, which is also called a callback (event handler = callback),
  2. the event loop:: is the flow of the program in Node.js non-blocking model of coding. Most developers since 1943 (source)*** to the present day are accustomed to sequential and blocking programming, and this is because, since 1943, most programming languages have supported an execution flow of a program that is sequential and blocking (from binary to C# or Julia). In Node.js, the flow is instead a loop and non-blocking. Node’s V8 engine is constantly executing a loop that checks if the events are “registered” (fancy word for declared) so that it can take some action via a function callback. If an event has come into the event queue. The event that gets put into the queue is an object that usually comes with data to be processed by the callback function.
  3. the event queue:: the place where the event loop looks to know when an event that is registered comes in. It has the declared events because when they arrive, they arrive at the queue. When the V8 engine collects an event, it gets processed in order of arrival, and V8 matches the event with the action it has to take.

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:

Untitled

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:

Untitled

Source:

import - JavaScript | MDN