I have a JavaScript program, (it's actually a TypeScript program), that reads a file chunk by chunk with a FileReader:
while (j < size) {
let blob = selectedFiles[i].slice(offset, offset + chunk_size);
let reader = new FileReader();
reader.addEventListener('load', async function () {
let wordArray = typeof reader.result === "string" ? CryptoES.lib.WordArray.create(new TextEncoder().encode(reader.result)) : CryptoES.lib.WordArray.create(reader.result);
let segment = CryptoES.AES.encrypt(wordArray, pass).toString();
segments.push(segment);
});
await reader.readAsArrayBuffer(blob);
offset += chunk_size;
j += chunk_size;
}
But when I process the segments Array later, it's empty.
If I add an alert() after the while loop, wait a little bit after it appears and then press 'OK', my array has all it's contents it's supposed to have. My async/await doesn't seem to work properly. I'm using it inside an async function. What did I wrong?
Thank you!
EDIT: If you have other improvements for this piece of code, let me know in the comments as I'm still not a pro :)