Callbacks concept in Node.js
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
- 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
May be interested
- Learn about the concept of QoS on routersin the following article, we will introduce and guide you to learn about the concept of qos - quality of service on router devices ...
- Invite to admire the beautiful Windows XP 2018 Concept with Fluent Design language that everyone must enjoykamer kaan avdan designer used the design language fluent design and the functions of windows 10 to create a windows xp concept version.
- Video concept of Samsung Galaxy S10 makes iPhone X look like an old phonealthough it is about to release the note 9 but s10 is making samsung fans anxiously look forward to the massive upgrade rumors on the galaxy s. line. perhaps it's time to say goodbye to the iphone.
- This is the iOS 14 operating system we all expectios 14 concept with new features that all users are expecting.
- What is a folder The concept of a file (File) and a folder (Folder) in a computerif you are new to the array of data stored on your computer, you will surely read through many specialized terms such as: file, folder. so what exactly are they, how they work, and what they mean on computers. in this article, what will the software tips explain to you folder? the concept of files (file) and folders (folder)
- Invite to admire the Concept Windows 11 with many unique features and design interface Mac OS stylebelow is a concept version of windows 11 coming from designer avdan, whose design has a bit of direction from mac os along with many unique new features such as the system-wide background mode, automatic mouse pointer change color according to usage environment, quick access ...
- See the unique Windows 10 concept with the smart interface design of the Taskbarin this concept, samuel ojeda unites the taskbar with the toolbar to simplify the windows 10 interface, allowing users to pin the system processes such as windows ink, screen brightness, time and even releases are games.
- LG reveals concept for its next flagship phonelg on thursday offered a hint about its future phone, with a few concept images showing a 'raindrop' rear camera design and 'front-back symmetrical curves'
- Invite the latest concept about the iPhone XI, with rabbit earsbased on leaker information from leaker steve hemmerstoffer about the iphone 2019 generation, youtube channel waqar khan created a new concept for the iphone xi that gave users a first look at this product.
- How to install NVM on Debiannvm stands for node version manager. with nvm, you can manage multiple node.js instances of nodejs and switch between them without uninstalling and reinstalling the node.