The immediate manifestation of node. JS asynchronous programming is callbacks.
Asynchronous programming relies on callbacks to implement, but can not say that the use of callbacks after the program is asynchronous, the callback function will be called after the completion of the task, node uses a large number of callback functions, node so the API is to support the callback function. The following are examples of blocking and non-blocking.
Block Code instance:
First create a file Input.txt, as follows:
This is a test file, O (∩_∩) o hahaha ~.
Create main.js with the following code:
var fs=require ("FS");
var data=fs.readfilesync (' Input.txt ');
Console.log (Data.tostring ());
Console.log ("The program execute to the end");
Non-blocking code:
Main.js's Code:
Fs=require (' Input.txt ');
Fs.readfile (' Input.txt ', function (error,data) {
if (error) return Console.error (Error);
Console.log (Data.tostring ());
});
Console.log ("The program execute to the end");
node. JS callback function