Async的简化版 实现异步任务的并行或顺序执行
发布于 9个月前 作者 coordcn 636 次浏览

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);
    }
  }
4 回复

Async series 任务顺序执行 如果下一个任务需要上一个任务的结果是怎么实现的?我的办法是在callback的基础上在加一个results,但是这样做就存在一个问题,客户如果更改results,就有可能会影响到其他任务。这个问题只能跟每个调用的客户做个约定,不允许更改results。

.waterflow or .auto

@alsotang 明白了,思路是一样的,这个只能靠约定来解决了。

回到顶部