Reputation: 3
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 to execute 3 function above : createDir() finished -> unZip(); finished -> copydata();
Upvotes: 0
Views: 201
Reputation: 114014
You basically need to execute the next function where you print your console.log. Basically this:
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");
});
}
}
But this is not very modular is it? It creates too much dependency between the functions so you can't reuse any of them. No problem! Javascript allows you to pass functions around as arguments:
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
}
});
}
}
So now you can call them like this:
createDir(function(){
unZip(function(){
copydata()
});
});
Alternatively with newer versions of node.js you can use promises. It basically does the same thing but with chainable syntax:
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');
}
});
}
Which you can now call like this:
createDir()
.then(unZip)
.then(copydata)
.then(function(){
console.log('all done!');
});
Upvotes: 1
Reputation: 747
You can use either promises or async package to accomplish this task.
If you prefer to use async package, you could use async.waterfall(...)
function, which takes your asynchronous functions and executes them in order. You can choose to import just this function with async-waterfall package.
Also if you choose async-waterfall
, you'll probably have to do something like this (I have not tested this code, so you'll probably have to do some editing to make it work for 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) {
// you callback
// will be executed after all functions finish
});
Upvotes: 1