Created
February 12, 2020 06:13
-
-
Save yoojinyoung/d6e615791f6dff6b72a7774c45ce6ab2 to your computer and use it in GitHub Desktop.
Using the FileReader API in async functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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