Code taste
Two versions that both work. Pick the one an expert would ship, and say why.
Search box results
A search box fetches results as the user types. Both versions show results on screen.
Version A
TypeScript
async function onQueryChange(q: string) {
const res = await fetch(`/api/search?q=${encodeURIComponent(q)}`);
setResults(await res.json());
}Version B
TypeScript
let controller: AbortController | null = null;
async function onQueryChange(q: string) {
controller?.abort();
controller = new AbortController();
try {
const res = await fetch(`/api/search?q=${encodeURIComponent(q)}`, {
signal: controller.signal,
});
setResults(await res.json());
} catch (e) {
if ((e as Error).name !== 'AbortError') throw e;
}
}Which would you ship?
Why? Pick every reason that holds.
Pick a version first.
1 of 8
All drills and your readiness