Skip to content

Reading from a file seems rather unlike existing prior art like fetch? #433

Open
@benlesh

Description

@benlesh

While I understand that fetch generally has fewer ways of getting http responses than something dealing with opening and reading files, this API -- with regards to reading files in particular -- is surprisingly divergent, IMO. I personally found the initial example to be a bit hard to follow, where I was bouncing around inside the code to figure out what was being read, what happens when it's read, and what's initializing the read.

I guess I would have expected something like fetch and how you read the body:

const response = await fetch('/some/image.png');

if (!response.ok) {
  throw new Error('Not good');
}

const reader = await response.body.getReader();

for await (const data of reader) {
  // do things with binary data here.
}

Where a file read that aligns more with the existing API might look like:

const files = await showOpenFilePicker(/* options */);

for (const file of files) {
  console.log(`Reading ${file.name}`);
  
  // Get a reader
  const fileReader = await file.content.getReader();
  
  // Read the binary data
  for await (const data of fileReader) {
    // do something with binary data here
  }
}

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions