Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: prefer-blob-reading-methods #1269

Closed
fisker opened this issue May 13, 2021 · 2 comments · Fixed by #2065
Closed

Rule proposal: prefer-blob-reading-methods #1269

fisker opened this issue May 13, 2021 · 2 comments · Fixed by #2065

Comments

@fisker
Copy link
Collaborator

fisker commented May 13, 2021

FileReader is kind of old, and it doesn't use promise. A better way to read file should be "blob reading methods"

Fail

const arrayBuffer = await new Promise((resolve, reject) => {
	const fileReader = new FileRead();
	fileReader.onload = () => resolve(fileReader.result)
	fileReader.onerror = () => reject(fileReader.error)
	fileReader.readAsArrayBuffer(blob);
});
const arrayBuffer = await new Response(blob).arrayBuffer();

Pass

const arrayBuffer = await blob.arrayBuffer();
@sindresorhus
Copy link
Owner

This is now accepted.

@jimmywarting
Copy link

most often when you want to display a simple preview from a file input then it's faster to just display a blob:url from using URL.createObjectURL (as dataURL takes time to encode/decode and uses more memory)

but you always need to be carful of when you are not revoking object urls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants