Async Async Programming of Nodejs

Source: Internet
Author: User

The first is the series function, it is the function of serial execution, a function array of each function, each function is completed before executing the next function, the example is as follows:

Async.series ({    one:function (callback) {        callback (null, 1);    },    Two:function (callback) {        Callback (null, 2);    }},function (err, results) {   });

The first parameter of the series function can be an array or a JSON object with different parameter types affecting the format of the returned data, such as the parameters in the example array, and the returned results should be such ' [.] '.

Waterfall (tasks, [callback])

There are many similarities between the waterfall and the series functions, in order to execute a set of functions sequentially, but the difference is that the value generated by each function is waterfall to the next function, and the series does not have this function, as shown in the following example:

Async.waterfall ([      function (callback) {       //task1       callback (null,1);           },function (Data,callback) {      //task1       Callback (null,2);     }],function (err,results) {      console.log (results);});

It is also important to note that the tasks parameter of waterfall can only be an array type.

Parallel (tasks, [callback])

The parallel function executes multiple functions in parallel, and each function 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, not the order of completion, as shown in the following example:

Async.Parallel ([    function (callback) {        callback (null, ' one ');    },    function (callback) {        callback (NULL, ' both ');    }],function (err, results) {});

The tasks parameter can be an array or a JSON object, and as with the series function, the results format returned will be different for the tasks parameter types.

Parallel (tasks, [callback])

The parallel function executes multiple functions in parallel, and each function 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, not the order of completion, as shown in the following example:

Async.Parallel ([    function (callback) {        callback (null, ' one ');    },    function (callback) {        Callback (NULL, ' both ');    }],function (err, results) {});

The tasks parameter can be an array or a JSON object, and as with the series function, the results format returned will be different for the tasks parameter types.

Async Async Programming of Nodejs

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.