zoukankan      html  css  js  c++  java
  • 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 的主要知识。

  • 相关阅读:
    JS中使用正则表达式封装的一些常用的格式验证的方法-是否外部url、是否小写、邮箱格式、是否字符、是否数组
    Java中操作字符串的工具类-判空、截取、格式化、转换驼峰、转集合和list、是否包含
    Cocos2d-x 2.0 自适应多种分辨率
    应用自定义移动设备外观
    为移动设备应用程序创建外观
    【2020-11-28】人生十三信条
    【2020-11-27】事实证明,逃避是下等策略
    Python 之web动态服务器
    Python 之pygame飞机游戏
    PHP 之转换excel表格中的经纬度
  • 原文地址:https://www.cnblogs.com/gongzhuiau/p/10082165.html
Copyright © 2011-2022 走看看