A search component keeps the last successful results visible while a newer search is pending. Each request has an increasing request ID. Its success handler checks whether the ID is still active, but its failure and cleanup handlers do not:
The problem
A search component keeps the last successful results visible while a newer search is pending. Each request has an increasing request ID. Its success handler checks whether the ID is still active, but its failure and cleanup handlers do not:
start(id, query):
activeId = id
loading = true
error = null
fetch(query)
.then(result):
if id == activeId: results = result
.catch(err):
error = err.message
.finally:
loading = false
Use this controlled schedule. R1 starts for ann and remains pending. R2 starts for anna and succeeds with [Anna Rao]; its cleanup finishes. R3 starts for anne and remains pending. Then R1 rejects with a network error and its cleanup runs. Identify the wrong visible state, specify the correct state at that point, and design a deterministic test and repair. All requests use the same account; authorization and cache-key scope are outside this fixture.
Reference answer
Then expect these follow-ups
If R3 also rejects, which error should be shown and when should loading clear?
If the component tracks a pending-request set, which entry may R1 cleanup remove?
Does aborting R1 remove the need for identity guards when rejection or completion races with cancellation?
How would you show retained results without labelling them as matches for the new query?
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.