美文网首页
Nodejs学习笔记-异步流程控制

Nodejs学习笔记-异步流程控制

作者: 莫名FCJ | 来源:发表于2017-10-24 17:29 被阅读25次

安装async

npm install async --g
报错:npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference
解决办法:yum -y install openssl
报错:Error: Cannot find module 'async'
解决办法:修改/etc/profile,export NODE_PATH=/usr/lib/node_modules/,source /etc/profile

代码:https://github.com/fengchunjian/nodejs_examples/tree/master/async

交叉执行

//normal.js
function oneFun() {
    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("twoFun执行完毕");
}

oneFun();
console.log("oneFun执行");
twoFun();
console.log("twoFun执行");
console.log("主进程执行完毕");

串行无关联

//series.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("出错", {one:"one执行完毕"});
                    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();
console.log("主进程执行完毕");

并行无关联

//parallel.js
var async = require("async");

function exec() {
    async.parallel({
        one : function(done) {
            ii = 0;
            setInterval(function() {
                console.log("aaa=" + new Date());
                ii++;
                if (ii == 3) {
                    clearInterval(this);
                    //done("出错", {one:"one执行完毕"});
                    done(null, {one:"one执行完毕"});
                }
            }, 1000);
        },
        two : function(done) {
            jj = 0;
            setInterval(function() {
                console.log("bbb=" + new Date());
                jj++;
                if (jj == 5) {
                    clearInterval(this);
                    done(null, {two:"two执行完毕"});
                }
            }, 1000);
        }
    }, function(err, rs) {
        console.log(err);
        console.log(rs);
    });
}

exec();
console.log("主进程执行完毕");

串行有关联

//waterfall.js
var async = require("async");

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 + ", bbb=" + new Date());
                jj++;
                if (jj == 5) {
                    clearInterval(this);
                    done(null, preValue + ", two执行完毕");
                }
            }, 1000);
        }
    ], function(err, rs) {
        console.log(err);
        console.log(rs);
    });
}

exec();
console.log("主进程执行完毕");

补充

串行无关联:aaa出错,bbb不会执行
并行无关联:aaa出错,bbb继续执行但不执行回调

参考文档

nodejs13_异步流程控制(n13_async)
http://www.yuankuwang.com/web/index.php?r=respool/resview&rpid=46
node.js教程13_异步流程控制(上)
http://edu.51cto.com/center/course/lesson/index?id=124538

相关文章

  • Nodejs学习笔记-异步流程控制

    安装async npm install async --g报错:npm: relocation error: np...

  • Async流程控制学习

    今天学习nodeJS深入浅出的第四章。node异步控制有提到Async的强大流程控制能力,我觉得有必要学习下,所以...

  • nodejs异步控制

    bluebird与原生Promise对象及bluebird模块的中文API文档 nodejs异步控制「co、asy...

  • Node学习目录

    深入浅出nodejs学习笔记 章节内容第一章node简介第二章模块机制第三章异步I/O第四章异步编程第五章内存控制...

  • 作用域、事件、异步

    作用域、事件、异步 1. 课程介绍 Ø CNPM(淘宝NPM) Ø NodeJS控制台 Ø NodeJS作用域(掌...

  • Nodejs 异步流程控制及若干细节

    今天接了个爬虫任务,主要是从网页上将数据爬下来,规整后导出到Excel。以前工作中的爬虫都是基于HttpClien...

  • 回调,同步异步

    nodejs学习笔记 标签:node 异步 回调 单线程 1. callback 最简单的回调 注意回调的理解:...

  • node 异步模式

    异步流程控制的包:Async Step

  • Nodejs异步流程框架async

    Nodejs异步流程框架async 标签(空格分隔): Node.js [TOC] Node.js 是一个基于 C...

  • AsyncJS 异步流程控制DEMO详细介绍

    AsyncJS 异步流程控制DEMO详细介绍 1. 基本流程 串行流程、并行流程、混合执行series, wate...

网友评论

      本文标题:Nodejs学习笔记-异步流程控制

      本文链接:https://www.haomeiwen.com/subject/mgjxpxtx.html