AniUI Academy
hard+350 XPPractice

Ignore stale async results

Requests do not come back in the order they were sent. A search for 'a' can answer after a search for 'ab', overwriting the results with older ones — a race condition users see as the list flickering back to the wrong thing.

Implement latestOnly(fn). It returns a wrapped function that calls fn on every invocation, passing its arguments through, and returns a promise. Only the most recent invocation may settle with fn's outcome. The moment a newer invocation starts, every earlier invocation whose promise is still pending rejects with new Error("Superseded"). When an outdated call's underlying promise finally settles, that result is discarded — and an outdated rejection must not surface as an unhandled rejection. Calls made after the previous one has already settled behave completely normally.

What it has to do

  • Call fn on every invocation, with the arguments passed through.
  • Reject a superseded pending call with an Error reading Superseded.
  • Settle the newest call with fn's own result, whenever it arrives.
  • Discard the outcome of outdated calls, including their rejections.
  • Behave normally for calls made after the previous one settled.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.