"P77 node. js" Learning
/// for handling multiple events corresponding to one listener
varCount = 0;varResults = {};varDone =function(key, value) {Results[key]=value; Count++; if(Count = = 3){ ///Render Pagerender (results); }};fs.readfile (Template_path,"UTF8",function(err, template) {Done ("Template", template);}); Db.query (SQL,function(err, data) {Done ("Data", data);}); Lion.get (Funciton (err, resources) {Done ("Resources", resources);});///2015-10-14 11:05:49///using partial functions to handle the relationship between Sentinel variables and third-party functionsvarafter =function(Times, callback) {varCount = 0, results = {}; return function(key, value) {Results[key]=value; Count++; if(Count = =Times ) {Callback (results); } };};varDone =After (times, render);///In fact this is the initialization of done, and also the Done property count, results///If another function is initialized with after, the count and results between the two are irrelevant. Done =function(key, value) {Results[key]=value; Count++; if(Count = =Times ) {Callback (results); }}emitter.on ("Done", done);///In fact,Emitter.on ("Done",function(key, value) {Results[key]=value; Count++; if(Count = =Times ) {Callback (results); }}) Emitter.emit ("Done", "template", template);//after hearing the event, the running function becomes:Emitter.on ("Done",function("Template", template) {functionbody;})
Node. JS Multi-Async Collaboration Scenario