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');
  • 相关阅读:
    Android Things专题 1.前世今生
    用Power BI解读幸福星球指数
    [leetcode]Simplify Path
    字段的划分完整的问题
    k-means算法MATLAB和opencv代码
    【Oracle】RAC下的一些经常使用命令(一)
    Java中经常使用缓存Cache机制的实现
    jenkins环境自动部署
    jenkins环境搭建
    springboot单元测试@test的使用
  • 原文地址:https://www.cnblogs.com/noper/p/6258123.html
Copyright © 2011-2022 走看看