System Design Quiz Practice
Active Recall Drill Session
← Exit SessionQuestion 1 of 1
hardSystem Design
In this debounced autocomplete fetcher, if a slow request for 'a' and a fast request for 'ab' are both fired, and 'ab' resolves FIRST, what happens when 'a' resolves LATER?
code
function createFetcher(fetchFn) {
let latestId = 0;
return function onQuery(q, onResults) {
const id = ++latestId;
fetchFn(q).then((res) => {
if (id === latestId) onResults(res);
});
};
}
const fetcher = createFetcher(async (q) => {
const delay = q === "a" ? 500 : 50;
await new Promise((r) => setTimeout(r, delay));
return `results-for-${q}`;
});
fetcher("a", (r) => console.log(r));
fetcher("ab", (r) => console.log(r));