This is an issue when using FileReader#readAsArrayBuffer() or FileReader#readAsText() without additional parameters, as
these can be replaced with simpler Promise-based alternatives.
The FileReader API was designed before promises became standard in JavaScript. It requires verbose callback-based code with event
listeners for 'load' and 'error' events.
The newer Blob#arrayBuffer() and Blob#text() methods return promises directly, making the code much cleaner and easier to
understand. They eliminate the need for:
This modernization improves code readability and reduces the chance of errors in promise handling. The newer methods are well-supported in modern browsers and provide the same functionality with less boilerplate code.
Using outdated FileReader patterns makes code unnecessarily complex and harder to maintain. The verbose callback-based approach increases the likelihood of errors in promise handling and makes the codebase less consistent with modern JavaScript practices.
Replace FileReader#readAsArrayBuffer() with the simpler Blob#arrayBuffer() method that returns a promise directly.
const arrayBuffer = await new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.addEventListener('load', () => {
resolve(fileReader.result);
});
fileReader.addEventListener('error', () => {
reject(fileReader.error);
});
fileReader.readAsArrayBuffer(blob); // Noncompliant
});
const arrayBuffer = await blob.arrayBuffer();