Fetch Interview Questions
4 curated fetch interview questions with detailed answers and examples.
← Browse all interview questionsCreate an AbortController per request, pass controller.signal to fetch, and call controller.abort() from the Stop button or a useEffect cleanup. The fetch promise or the pending reader.read() then rejects with an AbortError, which you catch and treat as a normal stop, keeping the text already shown. On the server, pass the incoming request signal on to the upstream LLM call so the model actually stops generating and billing.
response.body is a ReadableStream of Uint8Array chunks. Get a reader with getReader(), call read() in a loop until done is true, and turn each chunk into text with a single TextDecoder using decode(chunk, { stream: true }). Then buffer the text and parse only complete lines or events, because network chunks do not line up with tokens or messages.
Avoid inconsistent state in JavaScript apps by managing promise resolution order in concurrent async operations.
Fetch data inside useEffect with async/await, handle loading and error states, and avoid the stale closure and race condition bugs that affect real production code.