Callbacks in Node.js Bootcamp
In this part we will discuss Event loop in Nodejs,
privacy callbacks in Node.js and
cybersecurity NPM modules(Node Package Manager. We will try to ascertain why asynchronous callbacks is the key mechanism behind the nodejs architecture and its advantages over synchronous callbacks.
Event loop in Node.js
Event loop in Node.js are what allow JavaScript to perform non-blocking I/O operations. In fact, JavaScript is single threaded, which means that once an application has begun running, others cannot be executed until the previous operation is completed.
Kernels today come with built-in support for multi-threading. Often, multiple operations are executing in the background at the same time. When one of these operations is completed, the kernel can notify the JavaScript library, Node.js, so that the appropriate callback can be added to the poll queue that will eventually be dispatched to the appropriate callback.
There are 2 types or ways of programming in node.js which are
Synchronous Callbacks and
Asynchronous Callbacks
Callbacks in Node.js
When defining the key components which is callbacks in Node.js, the syntax offers a prohibition. In other words, the semantics of a callback function are always synchronous. Callbacks is the mechanism in which the program has to call the specific function after completion of a give set of instructions or the given set of code blocks containing the logic. Synchronous callbacks can block the event loop and restrict the server scalability. All methods functions in Node.js are written in such a way that they support callbacks.
Synchronous Callbacks in node.js
Synchronous Callbacks in node.js is a blocking call, where the thread is blocked to execute the functions in a synchronous way and the rest of the program execution needs to wait until the current function gives its result or until that call is over.
Asynchronous Callbacks in node.js
Asynchronous Callbacks in node.js is a non-blocking call, where the thread continues to execute the rest of the program, while the call/current function is executed separately the thread doesn’t needs to wait for the current function’s result in order to execute the next functions inside that thread.
A sample illustration to demonstrate Synchronous Callbacks in node.js
function getCallbackSync(callback)
callback("This is the first call");
console.log("Before displaying the first call");
getCallbackSync(function(display)
console.log(display);
);
console.log("After displaying the first call")
The output for the above code is:
> Before displaying the first call
> This is the first call
> After displaying the first call
At first we declared a function getCallbackSync with an argument named as "callback"
That argument will display the message "This is the first call" into the console.
Then we wrote a console log function(which is a core module) that will display the message "Before displaying the first call"
Then we simply tried to implement/call the getCallbackSync function to execute its output which takes in an argument named "display" and write the value which it receives by calling the "getCallbackSync" function. Then in console log function it will display the message "This is the first call"
Here we are calling the "getCallbackSync" function as a Synchronous Callback.
In order to call a function first we write its name which is ""getCallbackSync" in line 6 then inside the ( parentheses we start by providing an argument for the function which we do by (function(display).
After that we go inside the opening braces , in line 7 we write console log which will output the result which it receives by calling the "getCallbackSync" function. As per line 1
Remember that inside the "getCallbackSync" As per line 1 we passed the argument of the callback as "This is the first call", (in line 2, we pass an argument to the function inside a parentheses), this is then accepted as the argument when "getCallbackSync" in line 6 is called, and then the "display" parameter takes in the argument "This is the first call", which after that outputs in the console.
After that we wrote another console log function that will display the message "After displaying the first call".
So here at first "Before displaying the first call" will be printed on screen. As per line 4( since we only declared the function from line 1 to 3 it will be called when we call this function in line 6)
Then the program reaches line 6 and calls the "getCallbackSync" function which in turn prints the message "This is the first call". Here we should take note that the lines below the function call which is from line 9, the thread will stop executing itself until the "getCallbackSync" is executed completely which simply means that if there is a large program and large set of instructions which are waiting after and onwards line 9 all of them will get stuck until the "getCallbackSync" function finishes printing the message.
In this case we only had 1 line to be called which was 9th line which needs to display the message "After displaying the first call" into the console.
Consider a real world application when suppose the function in line 6 needs to perform a database query or a search operation in the database, the code after this function call will have to stop until the database query is finished which is the disadvantage of the Synchronous Callbacks in node.js.
This disadvantage is removed by the Non Blocking Asynchronous Callbacks where the rest of the code doesn’t needs to wait until the current function is executed. In other words line 9 does not need to wait until the function call in line 6 completes its task, or a program doesn’t needs to mandatorily wait for the database search operation or query operation to finish itself before executing next lines of the program.
This capability of node.js allows itself to write a highly scalable and realtime backend server.
To demonstrate this Non Blocking or the Asynchronous Callbacks consider the same program example we took earlier but this time let us make the getCallbackSync function to wait for 5000ms or 5 seconds before executing the next instruction.
function getCallbackSync(callback)
setTimeout(function () callback("This is the first call") , 5000);
console.log("Before displaying the first call");
getCallbackSync(function(display)
console.log(display);
);
console.log("After displaying the first call");
This will print –
> Before displaying the first call
> After displaying the first call
> This is the first call
Note that this time the line 9 which was to print "After displaying the first call" did not waited for 5 seconds in order to finish let the function "getCallbackSync" finish itself first.
In other words consider if there was a database operation that was needed to be performed in the function "getCallbackSync" which is suppose to be taking 5 seconds of time, the next instructions in after this function needs not to mandatorily wait for 5 seconds or whatever time it takes.
For those who have any questions regarding in which as well as how to make use of
LogicLatest, you are able to call us with the web-page.