Nodejs Asynchronous---Async

Source: Internet
Author: User

One: Asynchronous Basics

1. Because node. JS is inherently asynchronous, it is highly efficient and performance-strong.

Console.log (' hi! ' ); SetTimeout (function() {   console.log (' hello! ') );console.log (' wow! ');

Like this, the output: Hi

wow!

Hello!

It can be seen that Nodejs is asynchronous

2. Higher-order functions

The high-order function gives me the feeling of closure.

function Test (a) {    returnfunction(b) {        return +b;    }} var q=test (2) (3); Console.log (q);

The return value is a function that is passed as a parameter.

3. Partial function----------Personal understanding is closure

Assuming that a parameter or variable has been preset for function A, we call a to produce a new function B, and function B is what we call the partial function

function Test (a) {    returnfunction(b) {        Console.log (a+ "say" +B);}    } var q=test ("Tom") ("Hello");

Partial function: A factory function that creates a function, and customizes a new function by specifying some parameters.

But two is one of the closures.

Two. Async Async

Install Async-----First, that's not much.

NPM Install Async

1.series (Tasks,callback)

var async=require ("async"); Async.series ({    one:function (callback) {        callback (null,1)  ;    },    Two:function (callback) {        callback (null,2);    }},function (err, results) {    Console.log (results)} );

This executes each function in the order in which it was written, resulting in a collection of {one:1, two:2}

Note: 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

2,waterfall (Tasks,callback)

var async=require ("async"); Async.waterfall ([    function (callback) {        //task1        callback (null,1);    }, function (data,callback) {        console.log (data);        Callback (null,2);    }],function (err,results) {    console.log (results);});

As with the series () function, each function is executed sequentially, but the value produced after each function succeeds is passed to the next function, not results

Note: The tasks parameter of waterfall can only be an array type.

3.parallel function (tasks,callback)

var async=require ("async"); Async.Parallel ([        function (callback) {            callback (null, ' one ');        },        Function (callback) {            callback (null, ' both ');        }    ],    function (err, results) {            Console.log ( results);    });

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 in which the completion is performed

Note: 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.

4.parallelLimit (Tasks,limit,callback)

varAsync=require ("Async"); Async.parallellimit ([function(callback) {SetTimeout (function() {Callback (NULL, ' one '); }, 3000); },        function(callback) {SetTimeout (function() {Callback (NULL, ' both '); }, 1000); }    ],    2,//Set the number of functions to execute concurrentlyfunction(err, results) {Console.log (results); });

If limit is 1, then 4S executes, if limit is 2, then 3S is resolved.

5.whilst (Test,fn,callback)

The test parameter is a function that returns a Boolean result that determines whether the loop continues by returning a value that is equal to the condition of the while loop stop.

var async = require (' async '); var j = 0;async.whilst (    function () {        return j<3;    },    function (callback) {//Remember that there should be a callback function        J + +;        Console.log (' WHILECB ' +j);        Callback ();    },    function (err) {    })

Callback () is the condition that is reached and circulates.

6.iterator (Tasks)

var iter = async.iterator ([    function () {console.log (' 111 ');},    function () {console.log (' 222 ');},    function () {console.log (' 333 ');}]); var it1 = iter (); it1 ();

This feeling is very important, but the present understanding is not enough, is studying!

This blog is equivalent to course notes!

/*async Course: HTTP://WWW//COM/COURSE/543E1A4F032C7816C0D5DFA1

The content is. Hubwiz.
*/

  

Nodejs Asynchronous---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.