Block Code instance:
Main.js
var fs=require ("fs"); var data=fs.readfilesync ("1.txt"); Console.log (data.tostring ()); Console.log (" end of program execution ");
Non-blocking code instances:
Main.js
var fs=require ("fs"); Fs.readfile ("1.txt") , Function (err,data) { ifreturn console.error (err); Console.log (Data.tostring ());}); Console.log (" end of program execution ");
1.txt
Hello World
Execute Command: Node main.js
Blocking code results:
Hello World program Execution end
Non-blocking code results:
Program execution End Hello Word
Above two examples we understand the difference between blocking and non-blocking calls. The first instance finishes executing the program after the file has been read. The second instance we do not need to wait for the file to be read, so that you can read the file while executing the next code, greatly improving the performance of the program.
As a result, blocking is performed sequentially, not blocking is not required in order, so if you need to handle the parameters of the callback function, we need to write it inside the callback function.
Nodejs callback function