Currently, I am attempting to read a file uploaded by the user and convert it into a String using two functions.
The first function is handleFileInput:
handleFileInput(event){
setTimeOut(async()=>{
let abcd= await this.convertFileToString(this.file) //the file has been uloaded successFully at this point
console.log(abcd) //this prints the enitre fn given in the resolve method
},3000)
}
The second function is convertFileToString:
convertFileToString(file){
return new Promise((resolve, reject)=>{
let fileReader = new FileReader();
fileReader.readAsText(file);
resolve(fileReader.onload = (event) =>{
this.XMLAsString=fileReader.result as String
})
})
}
When printing the value of abcd
in the console, I receive the following output:
ƒ (event) {
_this.XMLAsString = fileReader.result;
}
I am still fairly new to the concept of async/await
and Promises
, and I understand that a promise is the only asynchronous thing I can await. My goal is to store the value of the uploaded file, converted to a String, in the variable abcd
. How can I achieve this? Do I need to return a promise, and if so, how do I access the value of the file read as a String and store it in abcd
?