Skip to the solution
MediumFrontendAsked in 20245 min read

Debounce async promise resolver

394 views

The problem

Problem, as an interviewer would state it: "Write debounceAsync(fn, wait) — like regular debounce, but fn returns a promise, and EVERY caller (even ones from earlier in the burst) needs to get back a promise that resolves with the eventual, single real result."

Examples:

Code
const search = debounceAsync(fetchResults, 300);
const [r1, r2, r3] = await Promise.all([search("r"), search("re"), search("rea")]);
// only ONE real fetchResults call happens (with "rea"); r1 === r2 === r3

Clarifying questions expected:

  • Should EVERY caller in the burst resolve, or only the last one (with earlier callers left hanging forever)?
  • What happens if the eventual real call rejects — do ALL pending callers reject too?
  • Does this need to interoperate with a plain, non-async debounce, or is it a separate implementation?

Code / implementation expected: Yes — real, direct proof that 3 rapid calls in a burst produce exactly 1 real underlying invocation, with all 3 callers' promises resolving to that identical result.

  • js-coding
  • utility
  • algorithm

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: debounceAsync() makes exactly one real underlying call for a 3-call burst, with all 3 callers resolving to the same result

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 115 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build a Simple Client-Side Router That Matches Path Patterns

Continue