Error using async and await with filereader Error using async and await with filereader typescript typescript

Error using async and await with filereader


If you want to do it with promises then you could use the Response constructor (part of fetch api)

async readFile(event) {  const file = event.target.files[0]  if (file) {    // return this.processFileContent(await file.text())    const data = await new Response(file).text()    this.processFileContent(data)  }}

Update

Now it's possible to just use blob.text() to return a promise that resolves with the text content, doe it's not available in all browser, see Browser Compatibility@MDN


Needed to leverage reader to convert blob to base64, prefer to use async-await syntax so I chose to extract reader logic into helper like this:

//* Convert resBlob to base64export const blobToData = (blob: Blob) => {  return new Promise((resolve) => {    const reader = new FileReader()    reader.onloadend = () => resolve(reader.result)    reader.readAsDataURL(blob)  })}

and calling it using await in main code:

//* Convert resBlob to dataUrl and resolveconst resData = await blobToData(resBlob)