A live-search page sometimes shows results for an older query. How would you fix it?
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
Responses can arrive out of order, so the last response received may belong to an earlier query. Track a monotonically increasing request identifier and update the UI only when it matches the current request. Abort previous requests where possible, but still guard against stale completions. Debounce input to reduce unnecessary requests, represent loading and error states for the current query, and clear invalid results deliberately. Test with artificial latency that makes earlier requests finish last. Debouncing alone does not solve the ordering problem.
Related Questions in JavaScript & Web Technologies
Explain the JavaScript Event Loop, Call Stack, Microtask, and Macrotask Queues
Intermediate level • 429 views
How do let, const, and var differ in scope and reassignment?
Easy level • 7 views
What is a closure? Show a practical example.
Easy level • 6 views
What is the output of synchronous code, a resolved promise, and setTimeout with zero delay?
Medium level • 6 views
How should fetch code handle HTTP errors and malformed JSON?
Medium level • 6 views