You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
asyncfunctionreadFile(file,{ signal }){varreader=newFileReader();consthandler=e=>reader.abort();signal.addEventListener('abort',handler,{once: true});reader.addEventListener('load',()=>signal.removeEventListener('abort',handler));reader.addEventListener('error',()=>signal.removeEventListener('abort',handler));constp=newPromise((resolve,reject)=>{reader.onload=(e)=>resolve(e.target.result);reader.onerror=(e)=>reject(reader.error);});reader.readAsText(file);returnawaitp;}
Possible suggested change, assuming signal support:
asyncfunctionreadFile(file,{ signal }){varreader=newFileReader({ signal });constp=newPromise((resolve,reject)=>{reader.onload=(e)=>resolve(e.target.result);reader.onerror=(e)=>reject(reader.error);});reader.readAsText(file);returnawaitp;}
Not speaking for the editors here, but for my part, I consider FileReader deprecated and replaced by the .stream(), .text(), and .arrayBuffer() methods.
Reacted by Kagami Sascha Rosylight, Benjamin Gruenbaum, Jimmy Wärting and Tristan Fraipont
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.
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.
Activity
domenic commentedon Nov 20, 2020
Not speaking for the editors here, but for my part, I consider
FileReaderdeprecated and replaced by the.stream(),.text(), and.arrayBuffer()methods.benjamingr commentedon Nov 20, 2020
@domenic Ah thanks, so in that case the ask should be to support signals in
.textand.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.
domenic commentedon Nov 20, 2020
Yeah, that would make sense to me. Indeed they cannot be aborted currently; you'd need to use
.stream()+TextDecoderStream.annevk commentedon Nov 20, 2020
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.domenic commentedon Nov 20, 2020
I mean, we could also add it to
Request/Response's methods. Aborting in that case would probably throw away the entire body.mkruisselbrink commentedon Nov 20, 2020
Adding an AbortController to
.text()and.arrayBuffer()seems reasonable to me. Agreed that I'd considerFileReaderdeprecated/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.jimmywarting commentedon May 19, 2025