zoukankan      html  css  js  c++  java
  • node异步流程控制async

    1.串行无关联:async.series(tasks,callback);     
    多个函数依次执行,之间没有数据交换,其中一个函数出错,后续函数不再执行     
    async.series({     
        one: function(callback){     
            callback(null, 1);     
        },     
        two: function(callback){     
            callback(null, 2);     
        }     
    },function(err, results) {     
        console.log(results);     
    });     
    例:交叉执行    
     //-------------n13_async.js------------------------------     
    function oneFun() 
    { 
        /* 
        setTimeout(function(){ 
     
        },1000); 
        */ 
        ii=0; 
        setInterval(function(){ 
            console.log("aaa="+new Date()); 
            ii++; 
            if(ii==3){ 
                clearInterval(this); 
            } 
        },1000); 
        console.log("oneFun"); 
    } 
    function twoFun() 
    { 
        jj=0; 
        setInterval(function(){ 
            console.log("bbb="+new Date()); 
            jj++; 
            if(jj==3){ 
                clearInterval(this); 
            } 
        },1000); 
        console.log("oneFun执行完毕"); 
    }  
        
    oneFun(0);console.log("oneFun执行");    
    twoFun();  
    console.log("twoFun执行");   
    console.log("主进程执行完毕");  
    //-------------n13_async.js------------------------------     
    var async = require('async');   
    function exec(){  
        async.series({   
            one: function(done){  
                ii=0;  
                setInterval(function(){  
                    console.log('aaa='+new Date());  
                    ii++;  
                    if(ii==3){  
                        clearInterval(this);  
                        done(null,{one:"one"});  
                    }  
                },1000);  
            },  
            two: function(done){  
                jj=0;  
                setInterval(function(){  
                    console.log('bbb='+new Date());  
                    jj++;  
                    if(jj>3){  
                        clearInterval(this);  
                        done(null,{two:"two"});  
                    }  
                },1000);  
            }  
        },  
            function(err,rs) {   
                console.log(err);  
                console.log(rs);  
            });  
    }  
    exec();     
         
    2.并行无关联:async.parallel(tasks,callback);     
    多个函数并行执行,最后汇总结果,如果某一个流程出错就退出     
    async.parallel({     
        one: function(callback){     
            callback(null, 1);     
        },     
        two: function(callback){     
            callback(null, 2);     
        }     
    },function(err, results) {     
        console.log(results);     
    });     
    3.串行有关联:waterfall      
    每一步执行时需要由上一步执行的结果当做参数.所以每一步必须串行等待     
    async.waterfall([     
        function (done) {     
         
            done(null, 'one');     
        },     
        function (onearg, done) {     
         
            done(null, onearg + '| two');     
        },     
        function (twoarg, done) {     
         
            done(null, twoarg + '| three');     
        },     
        function (threearg, done) {     
         
            done(null, threearg + '| four');     
        }     
    ], function (error, result) {     
        console.log(result);     
        console.timeEnd('waterfall');     
    })  
    例子: 
    function exec(){ 
        async.waterfall( 
            [ 
                function(done){ 
                    ii=0; 
                    setInterval(function(){ 
                        console.log("aaa="+new Date()); 
                        ii++; 
                        if(ii==3){ 
                            clearInterval(this); 
                            done(null,'one完毕'); 
                        } 
                    },1000); 
                }, 
                function(preValue,done){ 
                    jj=0; 
                    setInterval(function(){ 
                        console.log(preValue+"="+new Date()); 
                        jj++; 
                        if(jj==3){ 
                            clearInterval(this); 
                            done(null,preValue+',two完毕'); 
                        } 
                    },1000); 
                     
                } 
            ],function(err,rs){ 
                console.log(err); 
                console.log(rs); 
            }     
        ) 
    } 
        
    4.parallelLimit(tasks, limit, [callback])     
         
    parallelLimit函数和parallel类似,但是它多了一个参数limit。     
    limit参数限制任务只能同时并发一定数量,而不是无限制并发,     
         
    async.parallelLimit([     
        function(callback){     
            callback(null, 'one');     
        },     
        function(callback){     
            callback(null, 'two');     
        }     
    ],     
    2, //只允许同时有两个函数并行     
    function(err, results){     
        console.log(results);     
    });
    1.串行无关联:async.series(tasks,callback);     
    多个函数依次执行,之间没有数据交换,其中一个函数出错,后续函数不再执行     
    async.series({     
        one: function(callback){     
            callback(null, 1);     
        },     
        two: function(callback){     
            callback(null, 2);     
        }     
    },function(err, results) {     
        console.log(results);     
    });     
    例:交叉执行    
     //-------------n13_async.js------------------------------     
    function oneFun() 
    { 
        /* 
        setTimeout(function(){ 
     
        },1000); 
        */ 
        ii=0; 
        setInterval(function(){ 
            console.log("aaa="+new Date()); 
            ii++; 
            if(ii==3){ 
                clearInterval(this); 
            } 
        },1000); 
        console.log("oneFun"); 
    } 
    function twoFun() 
    { 
        jj=0; 
        setInterval(function(){ 
            console.log("bbb="+new Date()); 
            jj++; 
            if(jj==3){ 
                clearInterval(this); 
            } 
        },1000); 
        console.log("oneFun执行完毕"); 
    }  
        
    oneFun(0);console.log("oneFun执行");    
    twoFun();  
    console.log("twoFun执行");   
    console.log("主进程执行完毕");  
    //-------------n13_async.js------------------------------     
    var async = require('async');   
    function exec(){  
        async.series({   
            one: function(done){  
                ii=0;  
                setInterval(function(){  
                    console.log('aaa='+new Date());  
                    ii++;  
                    if(ii==3){  
                        clearInterval(this);  
                        done(null,{one:"one"});  
                    }  
                },1000);  
            },  
            two: function(done){  
                jj=0;  
                setInterval(function(){  
                    console.log('bbb='+new Date());  
                    jj++;  
                    if(jj>3){  
                        clearInterval(this);  
                        done(null,{two:"two"});  
                    }  
                },1000);  
            }  
        },  
            function(err,rs) {   
                console.log(err);  
                console.log(rs);  
            });  
    }  
    exec();     
         
    2.并行无关联:async.parallel(tasks,callback);     
    多个函数并行执行,最后汇总结果,如果某一个流程出错就退出     
    async.parallel({     
        one: function(callback){     
            callback(null, 1);     
        },     
        two: function(callback){     
            callback(null, 2);     
        }     
    },function(err, results) {     
        console.log(results);     
    });     
    3.串行有关联:waterfall      
    每一步执行时需要由上一步执行的结果当做参数.所以每一步必须串行等待     
    async.waterfall([     
        function (done) {     
         
            done(null, 'one');     
        },     
        function (onearg, done) {     
         
            done(null, onearg + '| two');     
        },     
        function (twoarg, done) {     
         
            done(null, twoarg + '| three');     
        },     
        function (threearg, done) {     
         
            done(null, threearg + '| four');     
        }     
    ], function (error, result) {     
        console.log(result);     
        console.timeEnd('waterfall');     
    })  
    例子: 
    function exec(){ 
        async.waterfall( 
            [ 
                function(done){ 
                    ii=0; 
                    setInterval(function(){ 
                        console.log("aaa="+new Date()); 
                        ii++; 
                        if(ii==3){ 
                            clearInterval(this); 
                            done(null,'one完毕'); 
                        } 
                    },1000); 
                }, 
                function(preValue,done){ 
                    jj=0; 
                    setInterval(function(){ 
                        console.log(preValue+"="+new Date()); 
                        jj++; 
                        if(jj==3){ 
                            clearInterval(this); 
                            done(null,preValue+',two完毕'); 
                        } 
                    },1000); 
                     
                } 
            ],function(err,rs){ 
                console.log(err); 
                console.log(rs); 
            }     
        ) 
    } 
        
    4.parallelLimit(tasks, limit, [callback])     
         
    parallelLimit函数和parallel类似,但是它多了一个参数limit。     
    limit参数限制任务只能同时并发一定数量,而不是无限制并发,     
         
    async.parallelLimit([     
        function(callback){     
            callback(null, 'one');     
        },     
        function(callback){     
            callback(null, 'two');     
        }     
    ],     
    2, //只允许同时有两个函数并行     
    function(err, results){     
        console.log(results);     
    });

    我们写的js代码就像是有一个国王(主线程),而nodejs给国王提供了很多"仆人"。早上,一个仆人叫醒了国王,问他有什么需要。 国王给他一份清单,上面列举了所有需要完成的任务,然后睡回笼觉去了。当国王回去睡觉之后,仆人才离开国王,拿着清单,给其它的仆人一个个布置任务。 仆人们各自忙各自的去了(异步执行),直到完成了自己的任务后,才回来把结果禀告给国王。国王一次只召见一个人(主线程开始执行),其它的人就在外面排着队等着。国王处理完这个结果后,可能给他布置一个新的任务,或者就直接让他走了,然后再召见下一个人。等所有的结果都处理完了,国王就继续睡觉去了。直接有新的仆人完成任务后过来找他。 这就是国王的幸福生活。

     
  • 相关阅读:
    mongoDB看这篇就够了
    放不下
    jmeter连接不上MySQL数据库的原因以及解决方法
    SecureCRT自动断开连接的解决方法
    Linux及Windows查看占用端口的进程
    网络基础知识
    selenium中driver.close()和driver.quit()的不同点
    day2_窗口句柄切换
    day6_异常捕捉
    day6_logging模块
  • 原文地址:https://www.cnblogs.com/RuMengkai/p/6486601.html
Copyright © 2011-2022 走看看