node.js - how to run 3 function sequentially? -


i have 3 function:

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");         });     } } 

i want execute 3 function above : createdir() finished -> unzip(); finished -> copydata();

you can use either promises or async package accomplish task.

if prefer use async package, use async.waterfall(...) function, takes asynchronous functions , executes them in order. can choose import function async-waterfall package.

also if choose async-waterfall, you'll have (i have not tested code, you'll have editing make work you):

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) {     // callback     // executed after functions finish }); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -