0

I have the following javascript code

    processEntry = function(entry){
        var dirReader =  entry.createReader();

        var hasFolders = false;
        var hasFiles = false;

        dirReader.readEntries(function(entries) {

            for (var i=0; i < entries.length; i++) {

                var currentEntry = entries[i]
                if (currentEntry.isFile){
                    if(currentEntry.name != 'DS_Store'){
                        hasFiles = true;
                        console.log(entry.name,'has files value is',hasFiles);
                    }
                } else if (currentEntry.isDirectory){
                    hasFolders = true;
                    console.log(entry.name,'has folders value is',hasFolders);
                }

            }
        });
        console.log(entry.name,'has files value is',hasFiles);
        console.log(entry.name,'has folders value is',hasFolders);

        //other code that depends on those values
}

The problem is, the first console log statements print correctly but for the second ones, they are always false. This means that the other code is never executed since the booleans say that all entries have no folders and no files. The entries are printing to the console correctly.

The entry is obtained by this line of code :

var entry = e.dataTransfer.items[i].webkitGetAsEntry();

I don't have much experience with Javascript.

  • Try moving those last `console.log` lines inside the function passed to `readEnteries`. It could be that `readEntries` is asynchronous. – Heretic Monkey Dec 08 '16 at 18:58
  • Thanks. Putting everything inside of the readEntries worked. – notThePenguin Dec 12 '16 at 22:28
  • Possible duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – Heretic Monkey Dec 12 '16 at 22:33
  • I'm voting to close this as a duplicate that's used for a number of "asynchronous problem" questions. Please don't feel like that's a bad thing; it's often difficult to spot these kinds of issues. I think this question will help others find that duplicate. – Heretic Monkey Dec 12 '16 at 22:35

0 Answers0