Tag: Handler ons post color function parameter file contents log
I. Blocking and non-blocking
The blocking is performed sequentially. There is a sequence of priorities.
Input.txt file content is: I was blocking
var=require("FS"); var= fs. Readfilesync(' input.txt '); Console. Log(data. ToString()); console. Log("program execution ends!") );
The output is:
I was blocking
End of program execution !
Rather than blocking is not required in order, so if you need to handle the parameters of the callback function, we need to write in the callback function.
Input.txt file content is: I am non-blocking
var fs = require ("FS"); Fs.readfile (thefunction (err, data) {if return console.error (err); Console.log (Data.tostring ());}); Console.log ("program execution ends!");
The output is:
Program execution ends!
I'm a non-blocking
two. Events
//Introducing the Events modulevarEvents = require (' Events ');//Create a Eventemitter objectvarEventemitter =Newevents. Eventemitter ();
*********************************************//1.To create an event handlervarConnecthandler =functionconnected () {Console.log (' Connection succeeded. ‘); }//2.binding Connection Event handlersEventemitter.on (' Connection ', Connecthandler);//3.Triggering connection EventsEventemitter.emit (' Connection '); *********************************************
* Pay attention to the order of writing in the split line, otherwise it will fail!
of course: Create an event handler without putting it in 2
Console.log ("program execution is complete. ");
Nodejs Getting Started learning notes