Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async 之流程控制 #3

Open
berwin opened this issue Nov 17, 2015 · 8 comments
Open

async 之流程控制 #3

berwin opened this issue Nov 17, 2015 · 8 comments
Assignees

Comments

@berwin
Copy link
Owner

berwin commented Nov 17, 2015

之前的老文章,换了个地方写博客,,所以得重新发布下~~

Control Flow

async是Nodejs中非常常用的一个工具模块,其中方法有很多,主要分3大类(集合,流程控制,工具),下面就简单说说 流程控制 的一些常用方法

series(tasks, [callback])

它的作用就是按照顺序依次执行。

async.series({
    one: function(callback){
        callback(null, 1);
    },
    two: function(callback){
        callback(null, 2);
    }
},function(err, results) {
    console.log(results);
});

输出 { one: 1, two: 2 }

async.series([
    function(callback){
        callback(null, 1);
    },
    function(callback){
        callback(null, 2);
    }
],function(err, results) {
    console.log(results);
});

输出 [ 1, 2 ]

上面写出两个,是因为series方法的第一个参数可以是一个数组也可以是一个对象,参数类型不同,callback 参数返回的值也不同。

waterfall(tasks, [callback])

waterfall和series函数有很多相似之处,都是按照顺序执行。
不同之处是waterfall每个函数产生的值,都将传给下一个函数,而series则没有这个功能,示例如下:

async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'
   console.log(result);
});

另外需要注意的是 waterfall 的 tasks 参数只能是 数组 类型。

parallel(tasks, [callback])

parallel函数是并行执行多个函数,每个函数都是立即执行,不需要等待其它函数先执行。
传给最终callback的数组中的数据按照tasks中声明的顺序,而不是执行完成的顺序,示例如下:

async.parallel([
    function(callback){
        callback(null, 'one');
    },
    function(callback){
        callback(null, 'two');
    }
],function(err, results){
    console.log(results); // [ 'one', 'two' ]
});

和series函数一样,tasks参数可以是一个 数组对象,tasks参数类型不同,返回的results格式也会不一样。

parallelLimit(tasks, limit, [callback])

parallelLimit函数和parallel类似,但是它多了一个参数limit
limit参数限制任务只能同时并发一定数量,而不是无限制并发,示例如下:

async.parallelLimit([
    function(callback){
        callback(null, 'one');
    },
    function(callback){
        callback(null, 'two');
    }
],2,function(err, results){
    console.log(results); // [ 'one', 'two' ]
});

以上是一些我个人比较常用的方法,还有一些其他不怎么常用的方法,建议去github看更多详细信息,不过是英文的哦~

@berwin berwin added the Blog label Nov 17, 2015
@berwin berwin self-assigned this Dec 14, 2015
@berwin berwin added the node.js label Apr 8, 2016
@qingmingsang
Copy link

与ES7的 async/await 区别是哪些?

@berwin
Copy link
Owner Author

berwin commented Jul 31, 2016

@qingmingsang 一个是原生支持的,一个是封装的,在这个类库比较火的时候还没有ES7的 async/await这种高级货。。。。。。。

@qingmingsang
Copy link

async/await 没有waterfall series 这些方法呀

@berwin
Copy link
Owner Author

berwin commented Aug 1, 2016

@qingmingsang async/await是基于promise来开发的 promise是可以做到waterfall series 这种情况的。。。

@qingmingsang
Copy link

我看了下,series 和await 功能相近。
promise.all和waterfall 功能相近。

@berwin
Copy link
Owner Author

berwin commented Aug 1, 2016

@qingmingsang promise.all 跟 parallel 功能是一样的。

@qingmingsang
Copy link

恩。相比起来似乎ES7 的 async 功能稍弱一些,不过也只是语法糖罢了

@berwin
Copy link
Owner Author

berwin commented Aug 1, 2016

@qingmingsang 功能肯定是类库提供的多。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants