zoukankan      html  css  js  c++  java
  • node基础13:异步流程控制

    1.流程控制

    因为在node中大部分的api都是异步的,比如说读取文件,如果采用回调函数的形式,很容易造成地狱回调,代码非常不容易进行维护。

    因此,为了解决这个问题,有大神写了async这个中间件。极大的方便了开发。

    这里主要介绍三种流程:窜行无关联,并行无关联,窜行有关联

    实例代码如下:

    /**
     * Created by gaoxiong on 2017/1/6.
     */
    var async = require('async');
    function exec(){
        //窜行无关联
        async.series({
            fn1:function(done){
                done('err', 'fn1 over')
            },
            fn2:function(done){
                done(null, 'fn2 over')
            }
        }, function(err, result){
            console.log(err);
            console.log(result);
        })
    }
    
    exec();
    console.log('all is done');
    /**
     * Created by gaoxiong on 2017/1/6.
     */
    var async = require('async');
    function exec(){
        //窜行无关联
        async.waterfall([
            function(done){
                var i = 0;
                setInterval(function () {
                    console.log('i'+i);
                    i++;
                    if (i == 3){
                        clearInterval(this);
                        done('fn1 err', 'fn1 over');//第一个异步的结果,是第二个异步的参数
                    }
                },1000)
            },
            function(preValue,done){
                var j = 0;
                setInterval(function(){
                    console.log('j'+j);
                    j++;
                    if (j == 3){
                        clearInterval(this);
                        done(null, preValue+'fn2 over');
                    }
                },1000)
            }
        ], function(err, result){
            console.log(err);//如果某个异步报错,那么会拿到done(prama1, param2),err为param1, result为param2
            console.log(result);
        })
    }
    
    exec();
    console.log('all is done');
    /**
     * Created by gaoxiong on 2017/1/6.
     */
    var async = require('async');
    function exec(){
        //并行无关联
        async.parallel({
            fn1:function(done){
                var i = 0;
                setInterval(function () {
                    console.log('i'+i);
                    i++;
                    if (i == 3){
                        clearInterval(this);
                        done('fn1 err', 'fn1 over');//并行时,假如这里错误,会影响其他异步拿到的结果,但是会继续执行代码
                    }
                },1000)
            },
            fn2:function(done){
                var j = 0;
                setInterval(function () {
                    console.log('j'+j);
                    j++;
                    if (j == 3){
                        clearInterval(this);
                        done(null, 'fn2 over');//虽然这里会打印2,但是这里不会希望拿到的结果
                    }
                },1000)
            }
        }, function(err, result){
            console.log(err);
            console.log(result);
        })
    }
    
    exec();
    console.log('all is done');
  • 相关阅读:
    C#委托、事件、消息(入门级)
    WinForm编程数据视图之DataGridView浅析(续)
    WinForm编程数据视图之DataGridView浅析
    在Java窗体表格中插入复选框
    单向链表(单链表)的Java实现
    C#委托、事件、消息(入门级)(续)
    Windows7操作系统自定义运行命令(简单方法之一)
    Unity的外部配置文件使用方法
    aspnet_regsql如何给sqlexpress添加aspnetdb
    联想Y530改装XP经验
  • 原文地址:https://www.cnblogs.com/noper/p/6258123.html
Copyright © 2011-2022 走看看