Skip to content

unicorn/prefer-blob-reading-methods Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

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.

Examples

Examples of incorrect code for this rule:

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);
    });
}

Examples of correct code for this rule:

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

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
    "rules": {
        "unicorn/prefer-blob-reading-methods": "error"
    }
}
bash
oxlint --deny unicorn/prefer-blob-reading-methods

References

Released under the MIT License.