如何顺序运行3个功能?

时间:2016-04-19 02:12:26

标签: node.js

我有3个功能:

function createDir(){
    fs.mkdirs('./quickstart',function(){
        console.log("thao thanh cong");
    });
}
function unZip(){
    fs.createReadStream('./prestashop_cache/' + quickstart).pipe(unzip.Extract({path:'./quickstart'}));
    console.log("giai nen xong");
}
function copydata(){
    if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
        fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
            console.log("coppy thanh cong toi thu muc data");
        });
    }
}

我想在上面执行3个函数:createDir()finished - >解压缩();完成 - >了CopyData();

2 个答案:

答案 0 :(得分:1)

您可以使用promises或async包来完成此任务。

如果您更喜欢使用异步包,可以使用async.waterfall(...)函数,该函数接受您的异步函数并按顺序执行它们。您可以选择仅使用async-waterfall包导入此功能。

此外,如果您选择async-waterfall,您可能需要执行此类操作(我尚未测试此代码,因此您可能需要进行一些编辑以使其适用于您):< / p>

var waterfall = require('async-waterfall');    

function createDir(callback){
    fs.mkdirs('./quickstart',function(){
        console.log("thao thanh cong");
        callback(null);
    });
}
function unZip(callback){
    var stream = fs.createReadStream('./prestashop_cache/' + quickstart).pipe(unzip.Extract({path:'./quickstart'}));

    stream.on('finish', function() {
        console.log("giai nen xong");
        callback(null);            
    });        

}
function copydata(callback){
    if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
        fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
            console.log("coppy thanh cong toi thu muc data");
            callback(null);
        });
    }
}

waterfall([
    createDir,
    unZip,
    copydata
], function(err) {
    // you callback
    // will be executed after all functions finish
});

答案 1 :(得分:1)

您基本上需要执行下一个打印console.log的功能。基本上这个:

function createDir(){
    fs.mkdirs('./quickstart',function(){
        console.log("thao thanh cong");
        unZip(); // CALL UNZIP WHEN WE ARE FINISHED
    });
}
function unZip(){
    var zipfile = fs.createReadStream('./prestashop_cache/' + quickstart);
    zipfile.pipe(unzip.Extract({path:'./quickstart'}));
    zipfile.on('end',function(){
        console.log("giai nen xong");
        copydata(); // CALL COPYDATA WHEN WE ARE FINISHED
    });
}
function copydata(){
    if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
        fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
            console.log("coppy thanh cong toi thu muc data");
        });
    }
}
但这不是很模块化的吗?它在函数之间创建了太多的依赖关系,因此您无法重用它们中的任何一个。没问题! Javascript允许您将函数作为参数传递:

function createDir(callback){
    fs.mkdirs('./quickstart',function(){
        console.log("thao thanh cong");
        if (callback) {
            callback(); // Call a function when finished
        }
    });
}
function unZip(callback){
    var zipfile = fs.createReadStream('./prestashop_cache/' + quickstart);
    zipfile.pipe(unzip.Extract({path:'./quickstart'}));
    zipfile.on('end',function(){
        console.log("giai nen xong");
        if (callback) {
            callback(); // Call a function when finished
        }
    });
}
function copydata(callback){
    if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
        fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
            console.log("coppy thanh cong toi thu muc data");
            if (callback) {
                callback(); // Call a function when finished
            }
        });
    }
}

所以现在你可以像这样打电话给他们:

createDir(function(){
    unZip(function(){
        copydata()
    });
});

或者使用较新版本的node.js,您可以使用promises。它基本上做同样的事情,但使用可链接的语法:

function createDir(){
    return new Promise(function(callback,error_callback){
        fs.mkdirs('./quickstart',function(){
            console.log("thao thanh cong");
            callback();
        });
    });
}
function unZip(){
    return new Promise(function(callback,error_callback){
        var zipfile = fs.createReadStream('./prestashop_cache/' + quickstart);
        zipfile.pipe(unzip.Extract({path:'./quickstart'}));
        zipfile.on('end',callback);
    });
}
function copydata(){
    return new Promise(function(callback,error_callback){
        if (path.extname(folder + '/samples' + version + '/data') != '.DS_Store' && path.extname(folder + '/samples' + version + '/data') != '.svn' && path.extname(folder + '/samples' + version + '/data') != '__MACOSX') {
            fs.copy(folder + '/samples' + version + '/data','./quickstart/prestashop/install/data/', function () {
                console.log("coppy thanh cong toi thu muc data");
                callback();
            });
        }
        else {
            error_callback('not found');
        }
    });
}

您现在可以这样称呼:

createDir()
.then(unZip)
.then(copydata)
.then(function(){
    console.log('all done!');
});