Request Lifecycle: Failures, Cancellation and Stale Results
A request-driven interface must distinguish loading, HTTP failure, malformed data, an empty result, success, and cancellation. Cancellation can stop supported work; a request identity check prevents an obsolete result from replacing current state.
The newest search owns the screen. An older response must not overwrite it just because that older response arrived later.
Separate the failure boundaries
A successful transport is not automatically a successful feature. A response can have a failing status, its body can contain invalid JSON, or its parsed JSON can have the wrong shape. Each check answers a different question. A zero-item array can be a valid empty result rather than an error.
A; version 1loadingReproduce a race without depending on a public server
This is a deterministic teaching transport, not a live HTTP service. Its slow and fast buttons intentionally use different delays and return real Response objects through the same asynchronous interface. Its cancellation listener stops the pending timeout and rejects with an AbortError. This makes request order and cleanup inspectable without an unreliable network.
Starting a request aborts the previous controller and gives the new request an increasing identity. Before writing any outcome, the handler checks that its identity is still current. The guard matters because some work cannot be cancelled, a response may already have arrived, or parsing and processing may continue after a network stage. An obsolete rejection must not replace current success with an old error either.
Test by starting Slow and then Fast immediately. Fast should own the final visible result. Aborting obsolete work is not a visible user failure. The demo does not send data to any external server or save anything in an account.
Apply the policy to a real endpoint
For a real fetch, pass the controller's signal in its options. Check response.ok before interpreting a body as successful data; await response.json; then validate the resulting shape. For a JSON POST, choose the endpoint's documented method and headers, serialize the body, and handle authentication according to that application's contract. Setting a Content-Type header does not make a server accept a schema it never defined.
A browser's same-origin and CORS rules are part of the host boundary. A frontend cannot grant itself cross-origin access just by adding an allow-origin response header to its request. An opaque no-cors response is not a shortcut to readable JSON. Distinguish a browser access restriction from an application-level 404 or invalid payload.
A retry policy needs limits and an understanding of the operation. Retrying a read can be reasonable; repeating a side-effecting request can duplicate work without a server-supported idempotency contract. Do not make every rejected promise retry forever.
Independent checks
Try success, empty and failure. Then run the Slow/Fast race several times. Inspect that the final button selected owns the screen rather than assuming completion order matches start order. The graded task separately checks the response classification policy with supplied status and parsed data. It is not a substitute for testing actual fetch transport behavior.