node. JS Multi-Async Collaboration Scenario

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.