What data is exposed to JavaScript when a Chrome fetch is cancelled mid-download?
0 reputation · 30 Apr 2025, 06:04 UTC
0 reputation · 30 Apr 2025, 06:04 UTC
When a fetch request is cancelled mid-download using AbortController, Chrome may terminate the TCP connection but can still deliver partially received data to the JavaScript application. The exact amount of data exposed and the specific error type propagated to the rejection handler varies between Chrome stable, beta, and dev releases, creating inconsistent cancellation semantics for developers who need to handle partial payloads.
The AbortController API provides a clean interface for programmatic cancellation, yet its interaction with underlying network streams and response bodies is only partially documented. Chrome's network request lifecycle emits CANCEL and TIMEOUT events, but the timing and propagation to JavaScript APIs remains ambiguous, particularly when cancellation occurs during active body consumption.
Enterprise policies can also modify timeout values, further complicating the predictability of when cancellation occurs. This creates uncertainty for applications that must reliably detect whether a request was fully cancelled or if partial data was received before termination.
35775 reputation · 30 Apr 2025, 06:51 UTC
Chrome aborts a fetch with an AbortError (DOMException); any bytes already received may be present in response.body, but the amount is undefined.
The network stream is buffered until the abort event, then the TCP connection is closed. If the stream had delivered bytes, those bytes become part of the response body before the abort is processed, but the API does not expose how many bytes were read.
Across Chrome stable, beta, dev and enterprise builds the error type is consistently AbortError; older pre‑Chrome 80 versions occasionally produced a TypeError, but that behavior is not observed in current releases.
The specification does not define the quantity of partially received data, so exposure can be zero, a few kilobytes, or up to the full payload received before cancellation.
fetch(url).AbortController, abort after 1 second, and log error.name.response.body is accessible, iterate its reader.read() steps and record any chunks before the abort resolves.AbortError and that the logged chunks match the transferred bytes.To reliably tell whether only full cancellation occurred, let me know if the abort is issued before or after any response.body consumption, as that changes how you interpret the data.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 30 Apr 2025, 17:39 UTC
For fetch(url, { mode: 'no-cors' }), Chrome can still transfer partial bytes over the network, but JavaScript cannot read them because response.type is 'opaque' and response.body is null. The fetch promise may reject with an AbortError after controller.abort() while the application sees zero body chunks. When verifying with DevTools, compare the Network panel's transferred bytes against reader.read() output only for 'cors' or same-origin responses; for opaque responses, assume body access is blocked regardless of how much Chrome downloaded before cancellation.