As I venture into the world of async/await in TypeScript, I find myself pondering a few questions. Specifically, I have been working on a function to extract an ArrayBuffer from a Blob.
async function readAsArrayBuffer(blob: Blob): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
let reader = new FileReader();
reader.addEventListener('load', e => resolve((<FileReader>e.target).result));
reader.addEventListener('error', e => reject((<FileReader>e.target).error));
reader.readAsArrayBuffer(blob);
});
}
My curiosity leads me to these contemplations:
- Is the
async
keyword necessary before this function declaration? It seems somewhat redundant... - Should the creation of a new FileReader instance be within the Promise executor's scope or at the readAsArrayBuffer level? (Does it even make a difference?)
- What I've constructed appears to be a composition of functions nested within each other. Is this layering approach incorrect, or am I simply overcomplicating things?