Concurrency and races · Chapter 8 of 15

The debounce that dropped the last edit

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Time
  • 3 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.js: lose the last edit on purpose

    Write reproduce() in repro.js. Make a pretend clock with createClock() from fake-clock.js and a reindex function that only records the name it is called with, in a list. Debounce it with debounce(reindex, 500, clock) from debounce.js. Call the debounced function with 'Ginger c', move the clock on 200 ms (await clock.tick(200)), call it with 'Ginger cha', move on 200 ms, call it with 'Ginger chai', then move on 2000 ms. Resolve to the list of names reindex was called with.

    The check runs your reproduce() twice: beside the original debounce.js it must resolve to ['Ginger c'] (the search got the first edit), and beside a fixed debounce.js it must resolve to ['Ginger chai']. No real waiting: the pretend clock moves at once, and reproduce() must finish within 2 seconds.

  2. Task 2

    debounce.js: run once, with the last call, after the burst

    Fix debounce(fn, ms, clock) in debounce.js. The function it returns may be called many times; a burst of calls makes one run of fn, ms after the last call of the burst, with the last call's arguments and this. Every call restarts the wait. So with ms = 400 and calls at 0, 300 and 600 ms, nothing runs before 1000 ms, and at 1000 ms fn runs once with the 600 ms call's arguments. A second burst after a quiet spell runs fn again, once, with its own last call.

    Keep the export, its three arguments and the real clock as the default; use only the clock's setTimeout and clearTimeout. The check moves its own pretend clock and looks at the exact moment fn runs, so a fix that keeps the last arguments but runs ms after the first call is wrong too.

  3. Task 3

    debounce.test.js: a test that would have caught it

    Write a vitest test file, debounce.test.js, that would have caught this bug. The check runs your file three times: beside the original debounce.js, where at least one test must fail; beside the fixed debounce.js, where every test must pass; and beside a wrong fix that keeps the last call's arguments but runs ms after the first call, in the middle of the burst, where at least one test must fail.

    So drive a burst of calls with the pretend clock from fake-clock.js and check both what and when: fn has not run yet a little before ms after the last call, and then it runs once, with the last call's arguments. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.js: lose the last edit on purpose
  • debounce.js: run once, with the last call, after the burst
  • debounce.test.js: a test that would have caught it

More in Time

Build The debounce that dropped the last edit yourself

Tapri's backend grew busy, and Kabir keeps handing you incidents that only happen when two things run at once. Each is a race interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.