Avoid multiple callbacks, node. js Async Module Async Early Use

Source: Internet
Author: User

Originally wrote a paged query, callback several layers.

Exports.list =function(req,res) {varparams = {}; varCurrent_page = Common_util.get_param_value (req, ' current_page ', ' number ', 1); varPage_size = Common_util.get_param_value (req, ' page_size ', ' number ', 10); varStart_index =Common_util.get_start_index (current_page,page_size); Topic.find (params). Skip (Start_index). Limit (page_size). Sort ({' Reply_time ': -1}). EXEC (function(err,docs) {if(Err) {console.error (err); }Else{topic.count (params,function(err,count) {if(Err) {console.error (err); }Else{Docs.foreach (function(DOC) {Doc.interval=Date_util.interval (doc.last_reply_time);                    }); Category.find ({},function(Err, categorylist) {if(Err) {console.error (err); } Else {                            varresult = {}; Result.current_page=Current_page; Result.page_size=page_size; Result.total_count=count; Resutl.total_page=common_util.get_total_page (count,page_size); Result.datas=docs; Result.categorylist=categorylist; Console.dir ("Hello");                            Console.dir (result); Res.render (' Index ', result);                }                    });        }            }); }        });}

After using async, the code looks like this:

Exports.list =function(req,res) {varparams = {}; varCurrent_page = Common_util.get_param_value (req, ' current_page ', ' number ', 1); varPage_size = Common_util.get_param_value (req, ' page_size ', ' number ', 10); varStart_index =Common_util.get_start_index (current_page,page_size); Async.Parallel ({datas:function(CB) {topic.find (params). Skip (Start_index). Limit (page_size). Sort ({' Reply_time ': -1}). EXEC (function(Err,docs) {CB (ERR,DOCS);       }); }, Total_count:function(CB) {Topic.count (params,function(Err,count) {CB (ERR,COUNT);       }); }, CategoryList:function(CB) {category.find ({},function(Err, categorylist) {CB (err,categorylist);       }); }    },function(err,results) {if(Err) {console.error (err); }Else{results.current_page=Current_page; Results.page_size=page_size; Results.total_page=common_util.get_total_page (results.total_count,page_size); Res.render (' Index ', results); }    });}

Async.Parallel, parallel execution. Supports JSON formatting tasks, the returned data is also in JSON format, and the order of JSON fields is sorted by task field.
{datas:[{_id:539c156dce6d3ea40f1f1d15,Author: ' Yekai ',content: ' <p>\n\t<span style= "font-family: Microsoft Jacob Black, Tahoma, Verdana, Arial Font-size:16px;line-height:26px;background-color: #FFFFFF;" > Because the site has just been created, and does not provide any special content, only the official homepage of the content is translated, but also a part of the error, but believe that the site will become an understanding of Ubuntu in China's dynamic window. </span>\n</p>\n<p>\n\t<span style= "font-family: Microsoft Jacob Black, Tahoma, Verdana, song body; font-size:16px; Line-height:26px;background-color: #FFFFFF; " ><br/>\n< /span>\n</p> ',Category: ' Other ',title: ' Ubuntu China website is now available ',__v:1,Last_reply_time:tue June21:54:33 gmt+0800 (China Standard Time),last_reply_username: ' Yekai ',Reply_amount:2,      },{_id:53a04500e5cb4710178753a5,last_reply_username: ' Yekai ',Last_reply_time:tue June21:44:52 gmt+0800 (China Standard Time),Author: ' Yekai ',content: ' Bet lost ',Category: ' World_cup ',title: ' Germany won ',__v:2,Reply_amount:3,      },{_id:539c16fe9e359e841cc1b097,Author: ' Yekai ',content: ' <p>\n\t<span style= "COLOR: #434343; font-family: Microsoft Ya-hei, Tahoma, Verdana, Arial Font-size:16px;line-height:24px;background-color: #FBFBFB;" > According to foreign media reports, researchers ' latest research shows that the Earth's largest aquifer is located beneath the U.S. mantle layer, about 643.7 kilometres (400 miles) underground. </span>\n</p>\n<p>\n\t<span style= "color: #434343; font-family: Microsoft Jacob Black, Tahoma, Verdana, song body; Font-size:16px;line-height:24px;background-color: #FBFBFB; " ><br/>\n< /span>\n</p> ',Category: ' Other ',title: ' The Earth's largest reservoir ' found 643.7 km underground,__v:2,Last_reply_time:tue June22:06:43 gmt+0800 (China Standard Time),last_reply_username: ' Afei ',Reply_amount:3,       } ],Total_count:3,categorylist:[{_id:5397181a1f7f534418cbbd84,Desc: ' Java class ',code: ' Java ',name: ' Java ',__v:0},{_id:53971d90e209ea6414b2819f,desc: ",code: ' Nodejs ',name: ' Nodejs ',__v:0},{_ID:5399B429A068211816605FBB,desc: ",code: ' World_cup ',name: ' World Cup ',__v:0},{_ID:539C1526CE6D3EA40F1F1D14,desc: ",code: ' Other ',name: ' Other ',__v:0}],Current_page:1,page_size:10,Total_page:1}

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.