So using native JS I can read a text based blob as follows:
const getBlob = () =>
let url = // some endpoint that returns a blob
let xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function(event) {
let blob = xhr.response;
let reader = new FileReader();
reader.addEventListener('loadend', (e) => {
let read = e.srcElement.result;
console.log(read); <-- text I want
});
let read = reader.readAsText(blob);
console.log(blob)
console.log(read)
}
xhr.open('GET', url);
xhr.send();
}
However, I would like my function to return the variable read
My question is if it is possible to rewrite this with async / await to do so.
I ask because I am not sure how that would work with reader.addEventListener(...)
which is inside xhr.onload