js001 ---- async

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 的主要知识。

posted on 2018-12-07 12:04  gongzhuiau  阅读(369)  评论(0编辑  收藏  举报