A lot of people get confused with the callback concepts. Here is the small topic to make you understand about the callback and uses Node world. It is very simple. Due to this feature, Node.js has captured the market so fast. Let’s discuss.
What happens in Synchronous program
In Synchronous programs, the code executes line by line. After finishing the execution of the current line, then moves to the next line. If any line contains a method which takes time to execute (it might be I/O operation etc), the program execution will halt on that line and waits for execution to complete. These idle time CPU cycles are wasted. Below is the example,
function validate(){ getData() // getData() a program , which take more than a minute to complete. console.log('End') } function getData() { setTimeout(function() { console.log('outside') }, 500); }
In above example, program will wait till “getData()” function executed completely . Then ,the program will continue to next line and executed rest of the code with returned value. But when it comes to asynchronous programs , story is completely different. It doesn’t wait.
Asynchronous Program
Asynchronous programming is designed for ensuring non-blocking execution. Nods.js also follow the same design. It doesn’t wait till the blocking code getting executed. It doesn’t follow sequencing. If any section of code taking time to execute, it processes other section code which is available to execute. This way it utilises the ideal time, which drastically reduces the execution time.
This asynchronous feature creates an issue like some data from file need to be processed and displayed in UI. But due asynchronous feature, the section of code display data in UI got executed before the availability of data from the file as reading file taking time. To overcome this issue, Node.js has callback concept.
Callback :
Callback is a concept is Node.js which make sure the certain section code doesn’t execute till other section of code executed completely. We will learn more about the callback through below example
function demo() { getData(function(data) { console.log(data) console.log('End') }); } function getData(callback) { setTimeout(function() { callback('Outside') }, 500); } demo();
Definition of getData() function contains callback parameter which will pass the process and send the data to calling the function. getData() function is called ,we used function(data). This function accepts the data and parameter sent by function definition.
For example, the function has one parameter data which will receive value “Outside”.
Rules in Callback :
Few rules Node.js follows,
- err : this will the first argument in the callback function. If any error occurs during execution, it will contain some error data, else will possess value null.
- Next parameters in the callback could be anything. If there is no error, then it will hold some value, else it will be null.
- Lets a callback function has two parameters, so the structure would be like below
–>Error: callback(‘400’, null) returns to called function function(err,data)
- err=400 and data=null
–> Successful : callback(null, ‘Successful’) returns to called function function(err,data)
- err=null and data=Successful
Node.js follow above callback structure in it’s all modules. It is a simple and valuable concept. you can try on the above example to understand more on this concept
Please feel free to comment and suggest.
One comment
Nicely explained
Thanks for such article