I am super confused why Line 2 is executing before Line 1.
I have a function base64 to convert uploaded files into base64. Then I am calling the function base64, and then trying to put the base 64 into a variable. However, the flow is going upside down and not working. Why is the line 2 executing before line 1
function base64(file, callback) {
var coolFile = {};
function readerOnload(e) {
var base64 = btoa(e.target.result);
coolFile.base64 = base64;
callback(coolFile);
}
;
var reader = new FileReader();
reader.onload = readerOnload;
var file = file[0].files[0];
coolFile.filetype = file.type;
coolFile.size = file.size;
coolFile.filename = file.name;
reader.readAsBinaryString(file);
}
var fileToSend;
base64($('input[type="file"]'), function (filedata) {
var convertedFile = filedata.base64;
console.log('\n file SB1 \t' + convertedFile);
fileToSend = convertedFile;
console.log ('Line 1 \t' + fileToSend);
});
console.log('\n Line 2 \t' + fileToSend);