# unicorn/prefer-blob-reading-methods

> Require `blob.text()` and `blob.arrayBuffer()` instead of a `FileReader`

A `FileReader` delivers its result in a `load` event, so you end up writing a handler for it, another for `error`, and often a promise around both. `blob.text()` and `blob.arrayBuffer()` return a promise that you can `await`. A `File` is a `Blob`, so it has them too.

The rule reports any call to a method named `readAsText` or `readAsArrayBuffer` with one argument.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/prefer-blob-reading-methods" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad */}
```js
function readText(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.addEventListener("load", () => resolve(reader.result));
    reader.addEventListener("error", () => reject(reader.error));
    reader.readAsText(file);
  });
}
```

{/* bad */}
```js
reader.readAsArrayBuffer(file);
```

Not reported:

{/* good */}
```js
function readText(file) {
  return file.text();
}

const bytes = await file.arrayBuffer();
```

`blob.text()` always decodes as UTF-8. A call that passes an encoding has no replacement, so it isn't reported:

{/* good */}
```js
reader.readAsText(file, "shift-jis");
```
