JavaScript From Zero · 45 min · 300 XP
Capstone: product search
Build a resilient asynchronous product search with loading, empty, error and stale-response states.
This independent capstone combines the whole course. The request function and HTML are supplied; state, DOM updates, event handling, async control flow and safe rendering are yours.
Async interfaces have more than success and failure. A request begins in loading, may produce results or an honest empty state, and may fail. The user can also submit again before the first request finishes. If an old response arrives last and overwrites the newer result, the interface is wrong even though every request succeeded.
let latestRequest = 0;
async function search(query) {
const request = ++latestRequest;
const results = await fetchResults(query);
if (request !== latestRequest) return; // a newer search already started
render(results);
}Keep status text in an element with role="status" so assistive technology announces loading and results without moving focus. Disable the submit button while the latest request is pending, but still make the handler correct if another submit is triggered. Render all product names with textContent.
Loading your workspace…