Callbacks in Node.js is simply a function that behaves asynchronously. What does this mean? It means that the function is called at the completion of each task.
Callbacks is very important in Node.js and therefore you need to understand it clearly.
Get the Node.js Course Outline here
How Callbacks Work
Take an example. You have a function that reads the content of a file in the disk. In normal (synchronous) functions, the body of this function starts executing. Then, after execution, control is returned to the main program. Meanwhile, the main program must wait for the function to complete. But in callbacks, once the function starts executing, the main program can run in parallel. So, it does not need to wait for the function to complete. So when the task is completed, that it, file is read from disk, then you need to specify a code that would execute. Somehow, the function is ‘called back’ again. That is where the name callback comes:
- first it is called at the start of execution
- then it is ‘called back’ at the end of execution
So we say that the code executes in a non-blocking way.
Examples of Non-Callback Code
To get it clear, we would write two piece of code. The first code would be a normal code without callback. Then we would write the same code using a callback.
Create a file using notepad and save it in your drive D. Write the following text inside:
The Tech Pro is always here to provide the best learning experience for Software Development Feel free to subscribe to the YouTube Channel, Kindson The Tech Pro. He'll be pleased to assist you!
Now, write this block of code in app.js. ( you have this from the previous lesson)
//Read content of file and write it to the output console.log("Begining of task"); var fs = require("fs"); var text = fs.readFileSync('D:/nodefiles/testinput.txt'); console.log(text.toString()); console.log("End of Program");
Run this code using the command:
PS D:\Documents\NodeJSTutorials1> node app.js
The output of the program would be as given below:
Understanding Callbacks in node.js The Tech Pro is always here to provide the best learning experience for Software Development Feel free to subscribe to the YouTube Channel, Kindson The Tech Pro. He is pleased to assist you! End of Program
Using a Callback
Now, we would do the same using a callback.
The syntax is as follows:
fs.readFile(path, function(err, data){ /* Code to execute */ });
In the code above, the ‘Code to execute’ is the code to run after the function completes while data is the data returned.
err is used when error occurs.
Interestingly, callback works in a similar way in AngularJs. (Learn AngularJs here)
Now the complete callback is given below.
var fs = require("fs") var path = "D:/nodefiles/testinput.txt"; fs.readFile(path, function(err, data){ if(err) return console.error(err); console.log(data.toString()); console.log("End of Program"); });
In this case, the program does not wait for the file to be read from disk. However, if the data return is to be used, then the block of code that uses it should be placed inside the callback.
Hopefully, this helps clarify callbacks.
[…] Node.js – Introduction to Callback […]
[…] Node.js – Introduction to Callback […]