is there any way i can fetch the result object of a FileReader() without getting through a function ?
i have made a sample code below:
HTML
is there any way i can fetch the result object of a FileReader() without getting through a function ?
No. FileReader()
is asynchronous. You can use Promise
to achieve expected result
var code = "lorem ipsum";
$("input[type='file']").change(function() {
var upload = this.files[0];
var p = new Promise(function(resolve) {
var reader = new FileReader();
reader.onload = function() {
code = reader.result;
// pass `div` as resolve `Promise` to `.then()`
resolve($("div").append("
"));
};
reader.readAsDataURL(upload);
});
p.then(function(elem) {
elem.append("
");
})
});