Skip to content

unicorn/prefer-blob-reading-methods Pedantic

🚧 An auto-fix is still under development.

What it does

Recommends using Blob#text() and Blob#arrayBuffer() over FileReader#readAsText() and FileReader#readAsArrayBuffer().

Why is this bad?

FileReader predates promises, and the newer Blob#arrayBuffer() and Blob#text() methods are much cleaner and easier to use.

Example

javascript
async function bad() {
  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);
  });
}

async function good() {
  const arrayBuffer = await blob.arrayBuffer();
}

References

Released under the MIT License.