Debounce / Async Request Coalescing
Problem
Implement two related utilities. debounce(fn, delay) returns a function that postpones calling fn until delay ms have passed since the last invocation. Separately, build a request de-duplicator so that rapid identical fetches share a single in-flight request instead of firing many.
Input / Output
- Input: a function to debounce (plus a delay), and a request key + fetcher for the coalescer.
- Output: a debounced wrapper; and a fetch wrapper that returns the same promise for identical concurrent calls.
Constraints
- Support cancellation of a pending debounced call.
- Identical concurrent fetches must issue exactly one network request (single-flight).
Example
- Typing quickly into a search box issues one debounced search after typing stops.
- Two identical
fetchUser(1)calls made before the first resolves share one promise and one request.
added …