Using the Process Control tool in Nodejs async

Source: Internet
Author: User

The contents of async are divided into three parts:
    • Process Control: Streamline handling of 10 common processes
    • Collection processing: How to work with data in a collection using asynchronous operations
    • Tool class: A few common tool classes

This article describes one of the simplest and most commonly used process control sections.
Since Nodejs is an asynchronous programming model, there are some things that are easy to do in synchronous programming and now become cumbersome. The process control of async is to simplify these operations.

Installation
install async --save -d
1.async.waterfall instances (multiple functions are executed sequentially and the previous output is the last input)

This function is named Waterfall (waterfall), you can imagine the waterfall from top to bottom, in order to execute several functions sequentially. The difference is that each function produces a value that is passed to the next function. If there is an error halfway, the subsequent function will not be executed. The error message and the results previously generated will be passed to the waterfall final callback.
Note that the function does not support JSON-formatted tasks.

Scenario: In the interface where the course is created, first parse the form and upload the avatar, then save the course to the database
async.waterfall([
function(cb) {
util.formParse(req,function(err,result){ //from解析
cb(err,result);
});
},
function(user, cb) {
courseCtrl.create(course,function(err,result){ //保存到数据库
cb(err,result);
})
}
], function (err, result) {
if(err) throw err;
var results = util.response.regOK;
results.item = result;
res.send(results);
});
2.async.parallel instances (multiple functions executing in parallel)

executes multiple functions in parallel, each of which executes immediately, without waiting for other functions to execute first . The data in the array passed to the final callback is in the order declared in the tasks, rather than in the order in which the completion is performed.
If a function fails, the result value of err and the function that has been executed is immediately passed to parallel final callback. The values of other functions that are not executed are not propagated to the final data, but are taken up in a position.
Support for tasks in JSON form, the final callback result is also in JSON form.

Scenario: In the Get Course list interface, get a list of courses and get the number of courses
async.parallel({
count:function(cb){
courseController.count( req.query ,cb); //课程数目
},
list:function(cb){
courseController.getList( req.query ,cb); //课程列表
}
},function(err,results){
res.send(results);
});
Request result
{
count:2,
list:[
{
courseid:xxx,
name:xxx
},
{
courseid:xxx,
name:xxx
}
]
}
3.async.auto instances (multiple functions have dependencies, some are executed in parallel, others are executed sequentially)

used to handle the execution of multiple tasks that have dependencies . For example, some tasks are independent of each other and can be executed in parallel, but some tasks depend on some other tasks and can only be performed when those tasks are completed. While we can use Async.Parallel and async.series to combine this functionality, the code can be quite complex if the relationship between tasks is complex, and it can be cumbersome to add a new task later. Using Async.auto at this point will do more with less.
If there is an error in the middle of the task, the error is passed to the final callback, and the data generated by all tasks (including those already executed) is ignored.

Application Scenarios:

The

Obtains user information and user-favorite videos based on UID (user ID) and course_id (course ID):
Gets the user information in the users table based on the UID,
based on course_id, gets the course information in the course table, including Teacher_
According to TEACHER_ID, the instructor information is obtained in the user table (the previous step is completed before execution);
Finally, return userinfo,courseinfo,tearcherinfo;

async.auto({
userInfo:function(cb){
userCtrl.getInfo({uid:req.query.uid}, function (err, result) { //用户信息
cb(err,result);
});
},
couseInfo:function(cb){
courseCtrl.getInfo(req.query.courseid, function (err, result) { //课程信息
cb(err,result);
});
},
tearcherInfo:[‘couseInfo‘, function(results,cb) {
userCtrl.getInfo({uid:results.couseInfo.teacherid},function(err,result){ //讲师信息
cb(err,result);
});
}]
},function(err,results){
if (err) throw err;
res.send(results);
});
Request result
{
userInfo:{
uid:xxx,
name:xxx
},
courseInfo:{
courseid:xxx,
name:xxx
},
tearcherInfo:{
uid:xxx,
name:xxx
}
}

Using the Process Control tool in Nodejs async

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.