Search code examples
javascriptfilereaderfileapi

How to make FileReader Asyncronous without Using FileReaderSync in Javascript?


I am using FileReader for reading multiple files and folders through drag and drop in chrome using javascript. It is working properly, but problem is that i want to call function after all file and folder read completes. I can't because it is asyncronous operation and FileReaderSync is not supported in chrome. So is there any way to doing this?

this is my code,

entry.file(function (file) {

                        var reader = new FileReader();
                        reader.onloadend = function (e) {
                            data.push(this.result);

                        };

                        reader.readAsText(file);
                    });

Solution

  • As an example of what can be used here. This is can't be used as "Copy&Paste" variant :

    var filesCount = "give here number of files";
    
    var callbackFunction = function(){
        if(data.length == filesCount ){
            Console.log( "Assume this as the end of all files reading" );
        }
    }
    entry.file(function (file) {
    
        var reader = new FileReader();
        reader.onloadend = function (e) {
            data.push(this.result);
            callbackFunction();
        };
    
        reader.readAsText(file);
    });