Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using the FileReader API in async functions
// https://simon-schraeder.de/posts/filereader-async/
function readFileAsync(file) {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = reject;
reader.readAsArrayBuffer(file);
})
}
async function processFile() {
try {
let file = document.getElementById('fileInput').files[0];
let contentBuffer = await readFileAsync(file);
console.log(contentBuffer);
} catch(err) {
console.log(err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment