0
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.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
lch
  • 4,569
  • 13
  • 42
  • 75

1 Answers1

0

Besides the very bad error handling, which is an open window to many bugs. You're basically executing an async method fs.stat inside a forEach loop.

The loop fires fs.stats filelist.length times, and then executing the callback, without the fs.stats results

EDIT : That's the way to do it without using async.js or promise, note that callback has first argument for error handling, and second argument for your desired files.

function getStatForFiles(index,callback){
    if(index < filelist.length){
        fs.stat(path.join(filepath,filelist[index]),function(error,stats){
            if(error){
                callback(error)
            }else{
                var obj = {};
                if(stats.isDirectory()){
                    obj.type='directory';
                }else{
                    obj.type='file';
                }

                obj.name=file
                console.log(obj);
                files.push(obj);
                getStatForFiles(++index,callback)
            }

        });
    }else{
        callback(null,files)
    }
}
getStatForFiles(0,callback)
yosiweinreb
  • 475
  • 4
  • 12