Perform synchronization first
Then put settimeout (function xx () {} into the stack
Then execute the callback function xx ()
/** * Created by Administrator on 2016/10/28.*///Nodejs Package operation file function fsvarFS = require (' FS ');//Synchronous Read//var str = "//str + = Fs.readfilesync ('./data/01 ', ' utf-8 ');//str + = Fs.readfilesync ('./data/02 ', ' utf-8 ');//str + = Fs.readfilesync ('./data/03 ', ' utf-8 ');//str + = Fs.readfilesync ('./data/04 ', ' utf-8 ');////Console.log (str);//Read code asynchronously/** Requirements: The first file read after the completion of the first to get the data*///fs.readfile ('./data/01 ', ' utf-8 ', function (err,data1) {//if (err) {//console.log ("file read error");//}else{//fs.readfile ('./data/02 ', ' utf-8 ', function (ERR,DATA2) {//if (err) {//console.log ("file read error");////}else{//fs.readfile ('./data/03 ', ' utf-8 ', function (err,data3) {//if (err) {//console.log ("file read error");//}else{//fs.readfile ('./data/04 ', ' utf-8 ', function (ERR,DATA4) {//if (err) {//console.log ("file read error");//}else{//Console.log (DATA1+DATA2+DATA3+DATA4);// }// });// }// });// }// });// }// });/** * Callback function nesting problem * Promise (meaning of commitment) the code that this object uses to execute the callback is stored state in progress (pendding) * has completed resolved has failed reject drop async code to Promise object execution * Promise.then then the meaning of the function to invoke according to this state*/functionfn1 () {varp =NewPromise (function(resovle,reject) {//in Progress-"completed //In-and- out failureFs.readfile ('./data/01 ', ' utf-8 ',function(err,data) {Console.log ("Code is executing 1"); Resovle (data); }) }); returnp;}functionfn2 () {varp =NewPromise (function(resovle,reject) {//in Progress-"completed //In-and- out failureFs.readfile ('./data/02 ', ' utf-8 ',function(err,data) {Console.log ("Code is executing 2"); Resovle (data); //the state has been completed }) }); returnp;}functionFn3 () {varp =NewPromise (function(resovle,reject) {//in Progress-"completed //In-and- out failureFs.readfile ('./data/03 ', ' utf-8 ',function(err,data) {Console.log ("Code is executing 3"); Resovle (data); //the state has been completed }) }); returnp;}//and then doing the next thing according to the state is equivalent to writing the asynchronous code in sync.//fn1 (). Then (function (data) {//Console.log (data.tostring ());//return fn2 ();//}). Then (function (data2) {//Console.log (data2.tostring ());//return Fn3 ();//}). Then (function (data3) {//Console.log (data3.tostring ());// });///** * Summary: 1 Promise is a built-in object that can be used on both front and back ends to be able to use the * 2 function: Elegant fix callback function nesting * 3 How to use * 1) pull out each step of the callback code out * 2) call to borrow Pro Mise feature to implement * ES5 (write callback rather painful) is set as standard in Es6 */functionfn1 () {varp =NewPromise (function(resolve,reject) {//Here to put the original callback code here to execute /** * Three states:---> has failed to finish*/Fs.readfile ('./data/01 ', ' utf-8 ',function(err,data) {if(Err) {reject (err); }Else{Resolve (data); } }) }); returnp;} FN1 (). Then (function(data) {Console.log (data); returnfn2 ();},function(Err) {Console.log (err);}) Promise.all ([Fn1 (), fn2 (), Fn3 ()]). Then ( function (Result) {Console.log (result);})
/** * Nodejs Learning Features: Learning the operation of the corresponding module*//** * 1 Event Module Events * A Introduction module * B Monitor event on * C Trigger event (man-triggered emit or certain behavior of server triggering data)*/ varEvents = require ("Events");//Eventemitter to record basic information about some eventsvarEventemitter =Newevents. Eventemitter ();//Use this objectEventemitter.on (' xx ',function() {Console.log (' Xxd ');});//Triggering EventsEventemitter.emit (' xx '));/** Promise New OUT * *//Resolve Callback nesting methods: * 1 The original layer nested asynchronous code out of the package into a function * 2 the extracted asynchronous code into the Promise inside the Promise inside receive a function as parameter * 3 through Then this method to access * React for future VUEJS virtual DOM * Above steps: Write the original nested (fairly low readability) code to make it more readable * * HTTP Web Service * FS File Module * Events Event module **/
JS execution sequence/promise gracefully resolves callback function nesting