Callbacks concept in Node.js
Callback has the same asynchronous property for a function. A callback function is called when completing a specific task. All Node APIs are written in the way of callback functions.
What is callback?
Callback has the same asynchronous property for a function. A callback function is called when completing a specific task. All Node APIs are written in the way of callback functions.
For example, a function to read the file starts with reading the file and returns the control so that the environment that executes the next command can execute. When the file I / O (read / write) file is completed, it will call a callback function, with the contents of the file as a parameter. Therefore there will be no blocking or waiting when reading / writing File. It makes Node.js more efficient, like having a higher number of requests without waiting for the results to return.
For example Blocking Code
Create a text line with the name input.txt with the following content
QTM la trang Web huong dan cac bai lap trinh hoan toan mien phi cho tat ca moi nguoi !!!!!
Create a js file named main.js with the following content:
var fs = require ( "fs" ); var data = fs . readFileSync ( 'input.txt' ); console . log ( data . toString ()); console . log ( "Ket process" );
Now run the following command to see the result:
$ node main . js
Result:
QTM is a Web page that contains all the scripts
Welcome to the world !!!!!!
Let's get married
For example, Non-Blocking Code
Create a file named input.txt with the following content:
QTM la trang Web huong dan cac bai lap trinh hoan toan mien phi cho tat ca moi nguoi !!!!!
Update main.js with the following code:
var fs = require ( "fs" ); fs . readFile ( 'input.txt' , function ( err , data ) { if ( err ) return console . error ( err ); console . log ( data . toString ()); }); console . log ( "Ket process" );
Now run main.js to see the result:
$ node main . js
Result:
Let's get married
QTM is a Web page that contains all the scripts
Welcome to the world !!!!!!
These two examples explain the definition of blocking and non-blocking calls. The first example shows that the program locks until it reads the file and continues to run only a few seconds later, the second program does not wait for the file to read and continues to print "Ket" after the same time. show program.
According to Tutorialspoint
Previous article: NPM in Node.js
Next: Event Loop in Node.js
You should read it
- 20 million people download fake ad blockers, containing malicious code on Google Chrome
- 4 useful browsers that integrate ad blocking features
- Adblock Plus filter can be exploited to run malicious code
- How to get Adguard free for 3 months
- Google Chrome blocks ads with tools available from February 15
- How to prevent .SVG images containing new malware on Facebook
- Block websites with bad content on iPhone Safari
- The best ad blocking software for computers
- Firefox will slow down the process of loading user tracking scripts
- How to use AdLock ad blocker to block ads on Chrome
- How to block ads on Safari with AdBlocker⁺
- How to use uBlock Origin block ads on Edge Chromium
Maybe you are interested
11 best compass apps for phones 10 Best At Home Workout Equipments That Will Keep You Fit What is a bidet? Why do Japanese people prefer to use intelligent washing hose rather than toilet paper? What is a ventilator that every country in the world is racing to have as much as possible? How to calibrate the compass for Android Great times to buy hi-tech