Lawfare Group → Knowledge Institute → Lawfare Academy
← CS 220 — Web Applications, SQL and Software Engineering
Weeks 3–5
JavaScript state, asynchronous requests and rendering
Learning objectives
- Explain and trace the mechanisms in javascript state, asynchronous requests and rendering.
- Apply the method to the worked example and changed-condition exercise.
- Identify assumptions, failure cases and the evidence needed to justify a result.
An interface is a state machine, even when written without a state-machine library. A request can be idle, loading, successful or failed; successful data can also be empty. Displaying unrelated content while a destination loads creates confusion because the user cannot distinguish navigation from an error. Render a destination-specific loading state and replace it with the requested content.
Promises represent work whose result is not yet available. await pauses the current asynchronous function, not the entire browser. A fetch promise can resolve for an HTTP error response, so inspect response.ok before interpreting the body as success. Handle malformed data separately from a failed connection. When two requests overlap, an earlier slower response can overwrite newer results. Use cancellation or a request identity check so the latest user intent wins.
When rendering untrusted text, use textContent or an escaping mechanism. Inserting external strings as HTML can turn data into executable markup. Keep state changes separate from visual effects where possible, making them easier to test. Define what happens when the user navigates away, presses twice or loses the connection. A spinner without a recoverable error state can leave the page indefinitely unusable.
Worked reasoning The incrementing identifier makes an older response ineligible to replace a newer request. HTTP errors and malformed arrays reach the error state. textContent avoids treating returned strings as markup. The example does not render individual orders or implement the API; it demonstrates the request-state boundary. A real retry button can call the function again.
Extended teaching and laboratory
A user can initiate request B while request A is still in flight. Network completion order need not match initiation order. Rendering whichever response arrives last can display stale state. Track the current request generation, and make success, error and loading transitions conditional on that generation. Cancellation can save work, but checking relevance is still useful because completion and cancellation can race.
Treat returned strings as data. textContent displays characters; inserting untrusted HTML can change document structure and introduce executable content in unsafe contexts. A valid JSON shape does not prove that an answer belongs to the currently selected customer.
Worked problem The user selects customer A, then B. B returns first with five orders; A returns later with two. Trace the correct UI state, including the old request’s error handler.
Step-by-step reasoning 1. Assign generation 1 to A and generation 2 to B. The current generation is 2.
- B can render five orders and clear its loading state because its generation is current.
- A’s success must not replace B’s list. Likewise, A’s later failure must not overwrite B’s success with an error.
- Verify the response object and render order descriptions as text. Keep selection identity, request identity and displayed data aligned.
Apply the idea
The user selects customer A, then B. B returns first with five orders; A returns later with two. Trace the correct UI state, including the old request’s error handler.
B returns an empty list while A is still running. What should appear, and why should an empty array not be treated as a fetch failure?
Read the worked reasoning
Render a successful empty state for B, not A’s previous data and not a network error. An empty collection is a legitimate result under the API contract. Ignore A when it completes. Test empty, malformed, denied and failed responses separately so their distinct meanings remain visible.
Check understanding
Why check response.ok after fetch resolves?
- HTTP errors can still produce a resolved fetch response
Feedback
Transport completion and application success are different conditions.
- Resolution guarantees a 200 status
Feedback
A server can respond with an error status successfully over the network.
- JSON parsing verifies authorization
Feedback
Parsing a body does not establish access rights.
Further reading
Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study MDN JavaScript fundamentals, DOM scripting and Fetch; CS50 Web JavaScript and User Interfaces. Trace loading, success, empty and error states.