test_waterfall1.js
var async = require('async');
// waterfall Test 1
// 参数: task数组、task全部完成后的最终函数.
// 将数组中的task按顺序执行,没有调用callback的话不会执行下一个task
// callback 第一个参数是error,第二个以后的参数传递给下一个task
async.waterfall([
function(callback) {
console.log("---------- task 1 ----------");
callback(null, '111', '222');
},
function(arg1, arg2, callback) {
console.log("---------- task 2 ----------");
console.log("arg1=" + arg1 + " arg2=" + arg2);
callback(null, '333');
},
function(arg3, callback) {
console.log("---------- task 3 ----------");
console.log("arg3=" + arg3);
callback(null, '444');
}
], function (err, result) {
if (err) {
console.log("something is wrong");
} else {
console.log("all the functions have completed");
}
console.log("err=" + err);
console.log("result=" + result);
});
/*
D:\asyncsample>node test_waterfall1.js
---------- task 1 ----------
---------- task 2 ----------
arg1=111 arg2=222
---------- task 3 ----------
arg3=333
all the functions have completed
err=null
result=444
*/
test_waterfall2.js
var async = require('async');
// waterfall Test 2
// callback 第一个参数如果不为null,将跳过之后的所有task直接执行最终函数
async.waterfall([
function(callback) {
console.log("---------- task 1 ----------");
callback(null, '111', '222');
},
function(arg1, arg2, callback) {
console.log("---------- task 2 ----------");
console.log("arg1=" + arg1 + " arg2=" + arg2);
callback("ERROR TEST", '333'); // !!!!!pass an error to callback, the next function is not executed
},
function(arg3, callback) {
console.log("---------- task 3 ----------");
console.log("arg3=" + arg3);
callback(null, '444');
}
], function (err, result) {
if (err) {
console.log("something is wrong");
} else {
console.log("all the functions have completed");
}
console.log("err=" + err);
console.log("result=" + result);
});
/*
D:\asyncsample>node test_waterfall2.js
---------- task 1 ----------
---------- task 2 ----------
arg1=111 arg2=222
something is wrong
err=ERROR TEST
result=333
*/
test_waterfall3.js
var async = require('async');
// waterfall Test 3
// 如果某个task中没有调用下一个task,以后的所有处理都不会被执行(包括最终函数)
async.waterfall([
function(callback) {
console.log("---------- task 1 ----------");
callback(null, '111', '222');
},
function(arg1, arg2, callback) {
console.log("---------- task 2 ----------");
console.log("arg1=" + arg1 + " arg2=" + arg2);
//callback(null, '333'); // !!!
},
function(arg3, callback) {
console.log("---------- task 3 ----------");
console.log("arg3=" + arg3);
callback(null, '444');
}
], function (err, result) {
if (err) {
console.log("something is wrong");
} else {
console.log("all the functions have completed");
}
console.log("err=" + err);
console.log("result=" + result);
});
/*
D:\asyncsample>node test_waterfall3.js
---------- task 1 ----------
---------- task 2 ----------
arg1=111 arg2=222
*/
test_waterfall.js
var async = require('async');
// waterfall
// http://caolan.github.io/async/docs.html#waterfall
async.waterfall([
function(callback) {
console.log("---------- task 1 ----------");
setTimeout(function() {
console.log('task 1 done');
callback(null, '111', '222');
}, 100);
},
function(arg1, arg2, callback) {
console.log("---------- task 2 ----------");
console.log("arg1=" + arg1 + " arg2=" + arg2);
setTimeout(function() {
console.log('task 2 done');
callback(null, '333');
}, 50);
},
function(arg3, callback) {
console.log("---------- task 3 ----------");
console.log("arg3=" + arg3);
setTimeout(function() {
console.log('task 3 done');
callback(null, '444');
}, 10);
}
], function (err, result) {
console.log("---------- all done ----------");
console.log("err=" + err);
console.log("result=" + result);
});
/*
D:\asyncsample>node test_waterfall.js
---------- task 1 ----------
task 1 done
---------- task 2 ----------
arg1=111 arg2=222
task 2 done
---------- task 3 ----------
arg3=333
task 3 done
---------- all done ----------
err=null
result=444
*/
test_series.js
var async = require('async');
// series
// http://caolan.github.io/async/docs.html#series
// 和watarfall相同,也是按顺序执行数组里的task,没有调用到callback不会执行下一个task
// 不同的是:所有的task只有一个参数callback。所有往下传递的参数都会被放入最终函数的数组results里
async.series([
function(callback) {
console.log("---------- task 1 ----------");
setTimeout(function() {
console.log('task 1 done');
callback(null, '111', '222');
}, 100);
},
function(callback) {
console.log("---------- task 2 ----------");
setTimeout(function() {
console.log('task 2 done');
callback(null, '333');
}, 50);
},
function(callback) {
console.log("---------- task 3 ----------");
setTimeout(function() {
console.log('task 3 done');
callback(null, '444');
}, 10);
}
], function (err, results) {
console.log("---------- all done ----------");
console.log("err=" + err);
console.log(results);
});
/*
D:\asyncsample>node test_series.js
---------- task 1 ----------
task 1 done
---------- task 2 ----------
task 2 done
---------- task 3 ----------
task 3 done
---------- all done ----------
err=undefined
[ [ '111', '222' ], '333', '444' ]
*/
test_parallel.js
var async = require('async');
// parallel
// http://caolan.github.io/async/docs.html#parallel
// 和series/waterfall不同,task是并列执行的,callback不是执行下一个task。
// 和series一样,参数都会被放入最终函数的数组results里
async.parallel([
function(callback) {
console.log("---------- task 1 ----------");
setTimeout(function() {
console.log('task 1 done');
callback(null, '111', '222');
}, 100);
},
function(callback) {
console.log("---------- task 2 ----------");
setTimeout(function() {
console.log('task 2 done');
callback(null, '333');
}, 50);
},
function(callback) {
console.log("---------- task 3 ----------");
setTimeout(function() {
console.log('task 3 done');
callback(null, '444');
}, 10);
}
], function (err, results) {
console.log("---------- all done ----------");
console.log("err=" + err);
console.log(results);
});
/*
D:\asyncsample>node test_parallel.js
---------- task 1 ----------
---------- task 2 ----------
---------- task 3 ----------
task 3 done
task 2 done
task 1 done
---------- all done ----------
err=undefined
[ [ '111', '222' ], '333', '444' ]
*/
test_parallel_obj.js
var async = require('async');
// parallel
// 并行处理的第一个参数可以是一个对象,最终函数的参数也不是数组而是一个带key的对象
async.parallel({
first: function(callback) {
console.log("---------- task 1 ----------");
setTimeout(function() {
console.log('task 1 done');
callback(null, '111', '222');
}, 100);
},
second: function(callback) {
console.log("---------- task 2 ----------");
setTimeout(function() {
console.log('task 2 done');
callback(null, '333');
}, 50);
},
third: function(callback) {
console.log("---------- task 3 ----------");
setTimeout(function() {
console.log('task 3 done');
callback(null, '444');
}, 10);
}
}, function (err, result) {
console.log("---------- all done ----------");
console.log("err=" + err);
console.log(result);
});
/*
D:\asyncsample>node test_parallel_obj.js
---------- task 1 ----------
---------- task 2 ----------
---------- task 3 ----------
task 3 done
task 2 done
task 1 done
---------- all done ----------
err=undefined
{ third: '444', second: '333', first: [ '111', '222' ] }
*/
分享到:
相关推荐
async.min.js
我在网上没找到这个文件的下载,忘记出处了,实在网页上复制过来的,亲测有效可用
虽然是为 Node.js 设计的,但是它也可以直接在浏览器中使用。 Async 提供了大约20个函数,包括 map, reduce, filter, forEach 等等,也有常用的异步流程控制模式,并行,瀑布等等。 标签:Async
使用Bootstrap-table和JQuery TreeGrid插件展示树形表格,实现异步加载大数据量。
var drawAsync = eval&...Jscex.compile("async", function () { while (true) { that.context.drawImage(that.offCanvas, 0, 0,that.canvas.width,that.canvas.height); $await(Jscex.Async.sleep(50)); } }))
11:01:33,545 DEBUG ThreadPoolAsynchronousRunner:730 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@5b28c08e -- Running DeadlockDetector[Exiting. No pending tasks.] 11:01:43,558 ...
cmsdk_ahb_to_apb_async.v
async.js 或独立使用的异步条件执行 示例 1:与 Async.js 系列一起使用 var async = require ( 'async' ) var ifAsync = require ( 'if-async' ) async . series ( [ foo , ifAsync ( predicate1 ) . and ( ...
异步调用_Async.rar异步调用_Async.rar
#Async.js# Async.js 是一个用于 javascript 的小型线程库。 我发现了一些其他线程库,但它们都非常复杂。 因此,我着手创建一个小型且易于使用的库。 Async.js 使用内联网络工作者,但将所有困难的东西从你身上抽象...
Ajax-async-js.zip,学习异步javascript,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页的情况下更改。
jquery-treeview.js 是一个免费开源的jQuery目录树插件,它可用于MVC开发,关于asp.net mvc 的使用,请参照《MVC引用教程》,docx格式的图文教程
async.each_map_eachSeries_mapLimit 在每个文件夹中,运行“npm install async” 在01文件夹中,使用async.each(messages, send, cb)的方法,任务并行执行。 在文件夹02中,使用async.map(messages,send, cb)的方法...
javascript 支持链式调用的异步调用框架Async.Operation.docx
Async.in.CSharp.5.0(2012.9
es6 async.vue
Title: C# Multithreaded and Parallel Programming Author: Rodney Ringler Length: 482 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2014-12-24 ISBN-10: 184968832X ISBN...
前端开源库-async.util.keyiteratorAsync.Util.KeyIterator,Async KeyIteratorHelper方法作为模块。
从适合async.auto()的输入对象中选择所需函数的特定子树(或子树的联合async.auto() 。 目标是使用async.auto简化编写异步工作流,替换否则代码将有条件地改变输入对象或函数将在执行前检查其先决条件的情况。 例子 ...