Hi Guest 1 HomePage 1 Jobs 1 WalkIn 1Articles  

 :) Latest Topic
Node.js Tutorial
Parts of Node.js
Features of Node.js
Install Node.js Windows
Install Node.js Linux
Node.js First Example
Node.js Console
Node.js JavaScript Learn
Node.js REPL
Node.js NPM
Node.js CL Options1
Node.js CL Option2
Node.js Globals
Node.js OS
Node.js Timer
Node.js Errors
Node.js DNS
Node.js Net
Node.js Crypto
Node.js TLS/SSL
Node.js Debugger
Node.js Process
Node.js Process Example
Node.js Process Example2
Node.js Child Process
Child Process EXEC
Child Process SPAWN
Child Process Fork
Node.js Buffers
Node.js Streams
Node.js Reading Stream
Node.js Writing Stream
Node.js Piping Stream
Node.js Chaining Streams
Node.js File System Part1
Node.js File System Part2
Node.js File System Part3
Node.js Path
Node.js StringDecoder
Node.js Query String
Node.js ZLIB
Node.js Assertion
Node.js V8
Node.js Callbacks
Node.js Events
Node.js Punycode
Node.js TTY
Node.js Web Module
Node.js Upload Files
Node.js Email Send
Node.js BuiltIn Modules
Node.js Assert Code
Node.js Assert Function
Node.js console.time()
Node.js console.table()
Node.js DNS Code
Node.js cipher.final()
Node.js URL.hash API
Node.js URL.host API
Node.js URL.origin API
Node.js MySQL
Folders in Node.js
Folders Node.js part3
Rename Folder Node.js
Remove Folder Node.js
 


  

 
Node.js Callbacks
Question Posted on 08 Apr 2024

Home >> Tutorial >> Node.js Tutorial >> Node.js Callbacks


Node.js Callbacks

Node.js Callbacks


When we say about the Node.js callback, it is an asynchronous equivalent for a function. And it is called whenever any of task is completed. And whne we say about the Node.js, here in Node.js callbacks are generally used. And all of the APIs of Node.js are mainly written in such a way that it will supports callbacks. To understand this we will take an example, let suppose when a function start reading of any file, it will return the control to execution environment immediately so that the next instrution can be executed.

When file I/O file operation is complete, it will call the callback function. SO that there is no blocking or wait for File I/O. And this feature will makes Node.js highly scalable, As due to this feature it can process high number of request without waiting for any function to return result.


Blocking Code Example


To understand this you need to follow these steps:-

(1)In first step create a text file which named as input.txt which is having below content:-
Crackyourinterview is online platform which will provide self learning tutorials on many technologies. And try to covers all topics.


(2)Now in this step create a JavaScript file which name as main.js which is having below code:-

var fs = require("fs");

var data = fs.readFileSync('input.txt');

console.log(data.toString());

console.log("Program Ended");



(3)Now in this step we open Node.js command prompt and execute the below code

node main.js


Once you run above command you will get the output as given below.


Node.js Callback

Non Blocking Code Example


To understand this you need to follow these steps:-

(1)In first step we will create a text file which name is input.txt which is having the below content:-
Crackyourinterview is online platform which will provide self learning tutorials on many technologies. And try to covers all topics.


(2)Now in this step we will create a JavaScript file which name is main.js which have the below code:-

var fs = require("fs");

fs.readFile('input.txt', function (err, data)

{

if (err) return console.error(err);

console.log(data.toString());

});

console.log("Program Ended");



(3)Now in this step we will open Node.js command prompt and execute the below code to run the file:-

node main.js


Once you run above command you will get the output as given below.


Node.js Nonblocking Callback


As per the above two example, you will get the details of blocking and non-blocking calls. Here in above first example it shows that programs blocks until it reads the file and then only it proceeds to end the program and on other hand in second example, program will not wait for file reading but it just proceeded to print "Program Ended" and same time program without blocking continues reading the file.

So we can say that, a blocking program executes very much in sequence. And we can easily implement the logic from programming point of view in block programs. But non-blocking programs does not execute in sequence, so in case a program needs to use any data to be processed, it should be kept with-in the same block to make it sequential execution.





Other Important Questions
Parts of Node.js
Install Node.js Linux
Node.js First Example
Node.js Globals
Node.js DNS







 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |