Skip to content

Support using AbortController with FileAPI #159

Description

@benjamingr

Hey,

It would be useful to use the web platform standard cancellation method to abort readers.
Other web platform code (like fetch) supports cancellation through signals.

Code before:

async function readFile(file, { signal }) {
  var reader = new FileReader();
  const handler = e => reader.abort();
  signal.addEventListener('abort', handler, { once: true });
  reader.addEventListener('load', () => signal.removeEventListener('abort', handler));
  reader.addEventListener('error', () => signal.removeEventListener('abort', handler));
  const p = new Promise((resolve, reject) => {
    reader.onload = (e) => resolve(e.target.result);
    reader.onerror = (e) => reject(reader.error);
  });
  reader.readAsText(file);
  return await p;
}

Possible suggested change, assuming signal support:

async function readFile(file, { signal }) {
  var reader = new FileReader({ signal });
  const p = new Promise((resolve, reject) => {
    reader.onload = (e) => resolve(e.target.result);
    reader.onerror = (e) => reject(reader.error);
  });
  reader.readAsText(file);
  return await p;
}

Activity

  1. domenic commented on Nov 20, 2020

    @domenic
    Contributor

    Not speaking for the editors here, but for my part, I consider FileReader deprecated and replaced by the .stream(), .text(), and .arrayBuffer() methods.

  2. benjamingr commented on Nov 20, 2020

    @benjamingr
    Author

    @domenic Ah thanks, so in that case the ask should be to support signals in .text and .arrayBuffer?

    Is it currently possible to abort an ongoing .text()?

    Since it returns a promise and does not take a signal parameter I don't see/know how to do that.

  3. domenic commented on Nov 20, 2020

    @domenic
    Contributor

    Yeah, that would make sense to me. Indeed they cannot be aborted currently; you'd need to use .stream() + TextDecoderStream.

  4. annevk commented on Nov 20, 2020

    @annevk
    Member

    It would be a bit unfortunate to lose the symmetry with Request/Response, but maybe that's okay. I guess these are already different in that you can invoke them multiple times.

  5. domenic commented on Nov 20, 2020

    @domenic
    Contributor

    I mean, we could also add it to Request / Response's methods. Aborting in that case would probably throw away the entire body.

  6. mkruisselbrink commented on Nov 20, 2020

    @mkruisselbrink
    Collaborator

    Adding an AbortController to .text() and .arrayBuffer() seems reasonable to me. Agreed that I'd consider FileReader deprecated/legacy, especially now all major browsers support the new methods.

    I agree that adding it to Request/Response (i.e. Body) as well would be ideal, but I don't think it would be too bad to only have it here either. It would still be true that you can do anything with these methods on Blob that you can do on other Bodys. And the reverse isn't true already.

  7. deleted a comment from ayalatech on Dec 1, 2023
  8. jimmywarting commented on May 19, 2025

    @jimmywarting
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