I'm trying to wrap my head around the reader object.
I have this function, currently I console.log(this.result)
, but I would like to return it instead after the event is triggered.
readFile = function(fs, filename){
fs.root.getFile(filename, {}, function(fileEntry){
//this object reads files.
var reader = new FileReader();
//register callback for read files
reader.onloadend=function(e){
//this is what i want to return
console.log(JSON.parse(this.result));
};
//read file-function
fileEntry.file(function(file){
reader.readAsText(file);
},errorHandler);
},errorHandler);
};
Simply retrurning it like this won't work
readFile = function(fs, filename){
fs.root.getFile(filename, {}, function(fileEntry){
//this object reads files.
var reader = new FileReader();
//register callback for read files
reader.onloadend=function(e){
//this is what i want to return
return JSON.parse(this.result);
};
//read file-function
fileEntry.file(function(file){
reader.readAsText(file);
},errorHandler);
},errorHandler);
};
console.log(readFile(foo, bar))
will output undeifined, it seems as though the returns get triggered before reader.onloadend
is run.