#45 · Races

Find the bug · #45 · Races Medium JavaScript

Search shows results for the old query

Search-as-you-type sometimes shows results for what you typed earlier, not what’s in the box.

On this page

The report

Bug report

The search box fetches results as you type. Sometimes, after typing “phone” quickly, the results shown are for “ph”: they arrive after the “phone” results and replace them.

The code

Read it first: what should it do? Then click the line where you think the bug is.

JavaScriptClick the line you think is wrong
// Search as you type: shows the results for whatever the user typed.
function makeSearchBox(search, show) {
  return async function onType(query) {
    const results = await search(query);
    show(query, results);
  };
}

Read it, then pick a line.

What it prints

Running it on a small example prints the output on the left. It should print the output on the right.

The example
// "ph" is slow to answer; "phone" is quick.
const search = (q) => new Promise((r) => setTimeout(() => r([q + ' results']), q === 'ph' ? 40 : 10));
const screen = [];
const onType = makeSearchBox(search, (q, results) => screen.push(results[0]));
onType('ph');
onType('phone');
await new Promise((r) => setTimeout(r, 80));
console.log('screen shows: ' + screen[screen.length - 1]);

It prints

screen shows: ph results

It should print

screen shows: phone results
Show the answer: the bug, the fix, and how you’d find it

The bug (line 5)

Line 5 shows whatever results come back, whenever they come back. Requests don’t finish in the order they were sent: if the older “ph” search is slower, its results arrive last and overwrite the newer “phone” results. Only show results if they’re for the latest query (or cancel the older request).

The fix

1// Search as you type: shows the results for whatever the user typed.2function makeSearchBox(search, show) {3  let latest = 0;3  return async function onType(query) {5    const mine = ++latest;4    const results = await search(query);5    show(query, results);7    if (mine === latest) show(query, results);6  };7}

How you’d find it

  1. Reproduce with a slow first request and a fast second one: the old results win.
  2. Log when each request starts and finishes: “ph” starts first and finishes last. The screen shows whoever finishes last.
  3. The code assumes answers arrive in order. Nothing guarantees that over a network.

The test that catches it

It fails on the code above and passes once it’s fixed. Keep it, so the bug can’t come back.

const search = (q) => new Promise((r) => setTimeout(() => r([q]), q.length === 1 ? 30 : 5));
const shown = [];
const onType = makeSearchBox(search, (q, results) => shown.push(results[0]));
onType('a');
onType('ab');
await new Promise((r) => setTimeout(r, 60));
assert.equal(shown[shown.length - 1], 'ab');

Where else this bites

  • Tabs that load data: switch tabs quickly and the first tab’s data appears on the second.
  • React effects that set state after an await without checking they’re still current.
Your progress stays on this device.