Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 1.18 KB

prefer-blob-reading-methods.md

File metadata and controls

45 lines (34 loc) · 1.18 KB

Prefer Blob#arrayBuffer() over FileReader#readAsArrayBuffer(…) and Blob#text() over FileReader#readAsText(…)

💼 This rule is enabled in the ✅ recommended config.

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

Fail

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);
});
fileReader.readAsText(blob);

Pass

const arrayBuffer = await blob.arrayBuffer();
const text = await blob.text();
fileReader.readAsText(blob, 'ascii');
fileReader.readAsDataURL(blob);