var fs=require('fs');
var path=require('path');
module.exports.getFiles=function(filepath,callback) {
var files=[];
fs.exists(filepath,function(exists){
if(exists){
fs.stat(filepath,function(error,stats){
if(error){}
else{
if(stats.isDirectory()){
fs.readdir(filepath,function(error,filelist){
if(error){}
else{
filelist.forEach(function(file){
var obj={};
fs.stat(path.join(filepath,file),function(error,stats){
if(error){}
else{
if(stats.isDirectory()){
obj.type='directory';
}
else{
obj.type='file';
}
obj.name=file
console.log(obj);
files.push(obj);
}
});
});
console.log("callback")
callback(files); //problem is here
}
});
}
}
});
}
});
}
I wrote a function the gets the list of files from the given path. i am sending a callback to this function. the callback function renders the response web page. But Callback is executed before completion of entire function. because of this i got empty response on the web page. is there any way to solve this.