Debounce a Function You Can Cancel, Typed

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write debounce(fn, ms): return a function that, each time it's called, waits until ms milliseconds pass without another call and then calls fn once with the latest arguments. Give it a cancel() method that drops a pending call. Type it so the debounced function takes exactly fn's parameters and returns nothing, and cancel is a method that takes no arguments.

Your code must pass

  • Calls fn once with the latest arguments
  • Waits before calling fn
  • cancel drops the pending call
  • Has no type errors
  • Takes fn's parameters
  • Returns nothing and has cancel