Nodejs:异步流程控制(上)

function oneFun() {
    // setTimeout(function () {
    //     console.log("a");
    // }, 1000);
    i = 0;
    setInterval(function () {
        console.log("aaa==" + new Date());
        i++;
        if (i == 3) {
            clearInterval(this);
        }
    }, 1000);
    console.log("oneFun");
}

function twoFun() {
    j = 0;
    setInterval(function () {
        console.log("bbb==" + new Date());
        j++;
        if (j == 3) {
            clearInterval(this);
        }
    }, 1000);
    console.log("oneFun执行完毕");
}

//oneFun();
//twoFun();

/*
1,串行无关联:async.series
2,并行无关联:async.parallel
3,串行有关联:waterfall
*/

//异步操作,串行无关联
//npm install async --save-dev
var async = require('async');
function exec() {
    async.series({
        one: function (done) {
            i = 0;
            setInterval(function () {
                console.log("aaa==" + new Date());
                i++;
                if (i == 3) {
                    clearInterval(this);
                    done(null, 'one完毕');
                }
            }, 1000);
        },
        two: function (done) {
            j = 0;
            setInterval(function () {
                console.log("bbb==" + new Date());
                j++;
                if (j == 3) {
                    clearInterval(this);
                    done(null, 'two完毕');
                }
            }, 1000);
        }
    }, function (err, rs) {
        console.log(err);
        console.log(rs);
    }
    )
}

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

猜你喜欢

转载自blog.csdn.net/u013101178/article/details/84728364