Loading homepage
Implement debounce(fn, wait) with cancel() and flush() methods. Repeated calls postpone execution until wait milliseconds after the most recent call. Users must be able to discard a delayed invocation or run it immediately.
This extends the Debounce challenge with explicit control over pending work.
Input: const delayed = debounce(value => value * 2, 100); delayed(3); delayed.flush(); Output: 6 immediately; no later duplicate invocation.
Input: delayed("first"); delayed("latest"); // advance by wait milliseconds Output: fn receives only "latest".
Input: delayed(3); delayed.cancel(); delayed.flush(); Output: undefined; fn is not invoked.
debounce(fn, wait) returns a function with cancel(): void and flush(): ReturnType<typeof fn> | undefined. Tests use a controlled clock instead of real delays.