Node.js异步流,详细见https://caolan.github.io/async/docs.html#parallel

1, async

用的比较多的是 waterfall, 瀑布流, 就是每个异步函数按照顺序执行。如果出错了,可以callback(new Error("xxxx"), Code.Fail);

async 最好用的流程控制方法, 可大大降低代码耦合度。 (一个函数只做一件事, async.waterfall则实现了一序列函数的异步组合)

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'
}); // Or, with named functions:
async.waterfall([
myFirstFunction,
mySecondFunction,
myLastFunction,
], function (err, result) {
// result now equals 'done'
});
function myFirstFunction(callback) {
callback(null, 'one', 'two');
}
function mySecondFunction(arg1, arg2, callback) {
// arg1 now equals 'one' and arg2 now equals 'two'
callback(null, 'three');
}
function myLastFunction(arg1, callback) {
// arg1 now equals 'three'
callback(null, 'done');
}

parallel async.parallel(tasks, callback)

tasks 并行运行函数集合, 而不必等到上一个函数完成, 如果任何函数发送错误, 会立刻执行回调函数,并返回错误信息; 若没有发生错误, 则会在所有tasks函数执行完毕之后用回调函数将结果返回。

async.parallel([
function(callback) {
setTimeout(function() {
callback(null, 'one');
}, 200);
},
function(callback) {
setTimeout(function() {
callback(null, 'two');
}, 100);
}
],
// optional callback
function(err, results) {
// the results array will equal ['one','two'] even though
// the second function had a shorter timeout.
}); // an example using an object instead of an array
async.parallel({
one: function(callback) {
setTimeout(function() {
callback(null, 1);
}, 200);
},
two: function(callback) {
setTimeout(function() {
callback(null, 2);
}, 100);
}
}, function(err, results) {
// results is now equals to: {one: 1, two: 2}
});

eachSeries(coll, iteratee, callbackopt)       跟each一样,不过 一次只运行一个异步操作,       The same as each but runs only a single async operation at a time.

map 跟each 类似。

// assuming openFiles is an array of file names and saveFile is a function
// to save the modified contents of that file: async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
}); // assuming openFiles is an array of file names
async.each(openFiles, function(file, callback) { // Perform operation on file here.
console.log('Processing file ' + file); if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err) {
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});

————————————————————————————————————————————————————————————

以上就是node.js async 的主要知识。

js001 ---- async的更多相关文章

  1. [C#] async 的三大返回类型

    async 的三大返回类型 序 博主简单数了下自己发布过的异步文章,已经断断续续 8 篇了,这次我想以 async 的返回类型为例,单独谈谈. 异步方法具有三个可让开发人员选择的返回类型:Task&l ...

  2. async & await 的前世今生(Updated)

    async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...

  3. [.NET] 利用 async & await 的异步编程

    利用 async & await 的异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/5922573.html  目录 异步编程的简介 异 ...

  4. [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程

    怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html  ...

  5. [.NET] 利用 async & await 进行异步 IO 操作

    利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html  序 上次,博主 ...

  6. [C#] 走进异步编程的世界 - 开始接触 async/await

    走进异步编程的世界 - 开始接触 async/await 序 这是学习异步编程的入门篇. 涉及 C# 5.0 引入的 async/await,但在控制台输出示例时经常会采用 C# 6.0 的 $&qu ...

  7. [译] C# 5.0 中的 Async 和 Await (整理中...)

    C# 5.0 中的 Async 和 Await [博主]反骨仔 [本文]http://www.cnblogs.com/liqingwen/p/6069062.html 伴随着 .NET 4.5 和 V ...

  8. await and async

    Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...

  9. C# await和async

    基础阅读:http://www.cnblogs.com/jesse2013/p/async-and-await.html 答疑阅读:http://www.cnblogs.com/heyuquan/ar ...

随机推荐

  1. Sql Server 2012数据库的安装【自己一点一点敲的】

    Sql Server 2012数据库的安装 1.到微软官网上下载 下载链接为:https://www.microsoft.com/zh-cn/download/details.aspx?id=2906 ...

  2. Android UnitTest FrameWork

    Android test suites基于Junit,可以直接使用Junit测试不使用android api的class,也可以使用android的Junit extensions测试android ...

  3. 问题集锦 ~ PS

    #画正圆 按住鼠标左键 + shift (+alt 从中心扩散) #透明背景 选中选区,图层转换为智能对象,栅格化,按 delete #抠图 魔术棒,套索工具 #填充选区颜色 Ctrl+Del #填充 ...

  4. Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合(转)

    原文  http://blog.csdn.net/songanling/article/details/22454973 最新版Struts2+Hibernate+Spring整合     目前为止三 ...

  5. hiho 172周 - 二维树状数组模板题

    题目链接 描述 You are given an N × N matrix. At the beginning every element is 0. Write a program supporti ...

  6. hiho1469 - 简单dp

    题目链接 题目大意: 从一个大正方形数组里面找一个小正方形,满足其中的每个位置上的数都恰好比他的左边的那个和上边的那个大1(如果左边或上边的那个不存在的话就无此要求). 比如 1 2 32 3 43 ...

  7. linux查看系统cpu信息

    # 查看物理CPU个数 cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l # 查看每个物理CPU中core的个数(即 ...

  8. 利用fabric批量安装kvm虚拟机及其xp

    公司一批PC机需要安装多个虚拟机跑任务,搞来搞去决定用centos7安装KVM来跑.于是先折腾了一下午,利用早先搭建好的cobbler给PC机安装OS.然后fabric批量部署. 环境:centos7 ...

  9. GDOI2017 再次酱油记

    Day 0 13:00 pm 啊...今天中午一点钟从ez出发,感觉吼有趣啊.出发前先大喊一声****,在书包里放一本党史,感觉玄学可以救命[滑稽] 15:00 pm 到达东莞,坐标:石龙名冠金凯悦大 ...

  10. mysql 定时每秒插入一条数据

    1.创建表 2.创建存储过程 CREATE PROCEDURE user()INSERT INTO user(name,sex) VALUES ('1111','1'); 3.创建定时器 CREATE ...