1.async.series
具备顺序执行中括号中的函数的功能,当某个函数执行结果不符合需求(即不正确),则执行回调函数,不执行后面的函数
串行无关联模式要求每一步执行成功后才能执行下一步流程.所以是一个同步编程思想
myAsync.series([
function createdb1(cb)
{ //注意这个回调函数cb,不调用的话,不会调用下一个函数,也不会执行到asyFinish
cliRedis1 = myRedis.createClient(6379, "192.168.74.139", {});
cliRedis1.on('ready', function (res) {
console.log("ready...connec to redis1.........");
});
cb(err, {'fish' : 'key', 'my' : 'test'}); //注意err为非0或非null时,运行到asyFinish
},
function createdb2(cb)
{
cliRedis2 = myRedis.createClient(6379, "192.168.74.139", {});
cliRedis2.on('ready', function (res) {
console.log("ready...connec to redis2.........");
});
//err = '0'
cb(err, cliRedis2);
},
function createdb3(cb)
{
cliRedis3 = myRedis.createClient(6379, "192.168.74.139", {});
err = -1
cliRedis3.on('ready', function (res) {
console.log("ready...connec to redis3.........");
});
cb(err, "xiaoyu");
}
], function asyFinish(err, values) { //values中包含所有err为0的(即函数执行结果正确)函数回调结果
console.log("finished async." + err);
})
2..parallel
parallel函数是并行执行多个函数,每个函数都是立即执行,不需要等待其它函数先执行。 传给最终callback的数组中的数据按照tasks中声明的顺序,而不是执行完成的顺序
3.waterfall
顺序执行函数,前一个执行结果,作为后一个函数的输入
console.time('waterfall');
myAsync.waterfall({
one: function (done) {
//处理逻辑
done(null, 'one');
},
two: function (onearg, done) {
//处理逻辑
console.log('-----', onearg);
done(null, onearg + 'two');
},
three: function (twoarg, done) {
//处理逻辑
done(null, twoarg + 'three');
},
four: function (threearg, done) {
//处理逻辑
done(null, threearg + 'four');
}
}, function (error, result) {
console.log(result);
console.timeEnd('waterfall');
})
4.atuo
用来处理有依赖关系的多个任务的执行。比如某些任务之间彼此独立,可以并行执行;但某些任务依赖于其它某些任务,只能等那些任务完成后才能执行。
虽然我们可以使用async.parallel和async.series结合起来实现该功能,但如果任务之间关系复杂,则代码会相当复杂,以后如果想添加一个新任务,也会很麻烦。这时使用async.auto,则会事半功倍。
如果有任务中途出错,则会把该错误传给最终callback,所有任务(包括已经执行完的)产生的数据将被忽略。
这里假设我要写一个程序,它要完成以下几件事:
- 从某处取得数据
- 在硬盘上建立一个新的目录
- 将数据写入到目录下某文件
- 发送邮件,将文件以附件形式发送给其它人。
分析该任务,可以知道1与2可以并行执行,3需要等1和2完成,4要等3完成。
myAsync.auto({
getData: function (callback) {
setTimeout(function () {
console.log('1.1: got data');
callback(null, "1 finished");
}, 400);
},
makeFolder: function (callback) {
setTimeout(function () {
console.log('1.1: made folder');
callback(null, "2 finished");
}, 200);
},
writeFile: ['getData', 'makeFolder', function (callback, dataInput) //dataInput 同时传入前2个函数的结果
{
setTimeout(function () {
console.log('1.1: wrote file');
callback(null, 'myfile');
}, 300);
}],
emailFiles: ['writeFile', function (callback, results) {
console.log('1.1: emailed file: ', results.writeFile); // -> myfile
callback(null, results.writeFile);
}]
}, function (err, results) {
log('1.1: err: ', err); // -> null
log('1.1: results: ', results); // -> { makeFolder: undefined,
// getData: undefined,
// writeFile: 'myfile',
// emailFiles: 'myfile' }
});
5.whilst
相当于while,但其中的异步调用将在完成后才会进行下一次循环。
这里有一点我很不明白,只能有3个函数吗?
var count1 = 0;
myAsync.whilst(
function () { //循环条件函数
console.log('test... ', count1);
return count1 < 1000
},
function (cb) { //只能有一个参数
console.log('1.1 count: ', count1);
count1++;
cb(); //必须要回调,否则循环不会继续
//setTimeout(cb, 1000);
},
function (err) {
console.log('1.1 err: ', err); // -> undefined
}
);
:
1. 广告地址映射参考confluence:
confluence.24money.com/pages/viewpage.action?pageId=5211825
2. 广告类型选择请参考:
confluence.24money.com/pages/viewpage.action?pageId=5210235
3. 后续运营有个关于广告统计的需求,到时候在细说。