tasks
- async tasks for node.js
- Async的简化版,主要用于原理研究,测试不充分,请不要用于产品。
- EventProxy实现原理也是类似的,老赵给出了一个简化版,原理一看就明白了。
Download
Documentation
- 特别要注意的是函数的定义必须使用
- function name(callback){}
- 不要使用
- var name = function(callback){};
- 应该是由于lazy compile的原因,这样写函数会出现未定义的错误。
parallel(tasks, callback) 并行任务执行
Arguments
- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) tasks {object} 任务
function task(callback){
fs.readFile(path, function(err, data){
if(err){
callback(err);
}else{
callback(null, data);
}
});
}
- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) callback {function} 任务全部完成后执行的回调函数
function callback(err, results){
if(err){
handle(err);
}else{
handle(results);
}
}
Example
parallel({
test0:test0function,
test1:test1function
}, callback);
function test0funtion(callback){
fs.readFile(path0, function(err, data){
if(err){
callback(err);
}else{
callback(null, data);
}
});
}
function test1funtion(callback){
fs.readFile(path1, function(err, data){
if(err){
callback(err);
}else{
callback(null, data);
}
});
}
function callback(err, reuslts){
if(err){
console.log(err);;
}else{
console.log(results.test0);
console.log(results.test1);
}
}
series(tasks, callback) 顺序任务执行
Arguments
- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) tasks {object} 任务
function task(callback, reuslt){
var path = result;
fs.readFile(path, function(err, data){
if(err){
callback(err);
}else{
callback(null, data);
}
});
}
- [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[@param](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param)](/user/param) callback {function} 任务全部完成后执行的回调函数
function callback(err, result){
if(err){
handle(err);
}else{
handle(result);
}
}
Example
series({
test0:test0function,
test1:test1function
}, callback);
function test0funtion(callback, results){
fs.readFile(path0, function(err, data){
if(err){
callback(err);
}else{
callback(null, data);
}
});
}
function test1funtion(callback, results){
var path1;
if(!results.test0){
callback('reuslts.test0 not defined');
}else{
path1 = results.test0;
fs.readFile(path1, function(err, data){
if(err){
callback(err);
}else{
callback(null, data);
}
});
}
};
function callback(err, reuslts){
if(err){
console.log(err);;
}else{
console.log(results.test0);
console.log(results.test1);
}
}