Please check the following code:
function loadCSV(fileInput) {
var reader = new FileReader();
reader.readAsText(fileInput);
var result = {};
var file = {};
var error = {};
reader.onload = function(evt) {
var csv = evt.target.result;
var data = $.csv.toArrays(csv);
file[fileInput.name] = data;
result['file'] = file;
console.log(result);
}
reader.onerror = function() {
error = {
'message': 'Unable to read ' + fileInput.name
}
result['error'] = error;
}
console.log(result);
return result;
}
The result
within the reader.onload
section is correct, but the result
returned is empty. I know this is because I am updating the result
in a sub-function reader.onload
and I also know using callback for loadCSV
is a solution to solve the problem. But I want to know whether there is any way that I can make loadCSV
returning the updated result
.
Thanks.