Skip to the solution
MediumFrontendAsked in 20244 min read

Throttle async promise resolver

3.3k views

The problem

Problem, as an interviewer would state it: "Write throttleAsync(fn, wait) — like regular throttle, but wraps an async fn. If fn is already IN FLIGHT (a real, pending call), additional calls should not start a redundant second real call — they should share the existing in-flight result."

Examples:

Code
const t = throttleAsync(fetchStatus, 200);
const [a, b, c] = await Promise.all([t(), t(), t()]);
// only ONE real fetchStatus() call happens; a, b, c all resolve to its result

Clarifying questions expected:

  • If a call arrives WHILE another is genuinely in flight, does it wait and share that result, or is it dropped entirely?
  • Does the throttle window start when the call BEGINS, or only once it genuinely finishes?
  • Should a call that arrives just after the window closes trigger a fresh, new real call?

Code / implementation expected: Yes — real, direct proof that 3 near-simultaneous callers, while a call is already in flight, produce exactly 1 real underlying invocation.

  • 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

4 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: throttleAsync() makes exactly one real underlying call when 3 callers arrive while a call is already in flight

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 57 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

Implement omit(obj, paths) Supporting Both Shallow and Nested Paths

Continue