AniUI Academy
medium+250 XPPractice

Poll until a condition holds

Plenty of backends hand you a job id and expect you to check back: video encoding, report generation, payment confirmation. The client keeps asking until the status flips, and must stop asking eventually.

Implement pollUntil(check, options) where options may contain interval (default 50) and timeout (default 2000). Call check() immediately; it may return a value or a promise. If the awaited result is truthy, resolve with that exact value. Otherwise, if at least timeout milliseconds have elapsed since the first call, reject with new Error("Polling timed out"). Otherwise wait interval milliseconds and call check() again. check is never called again after the returned promise settles.

What it has to do

  • Call check once immediately, before any waiting.
  • Resolve with the first truthy result, exactly as returned.
  • Await results, so an async check works.
  • Wait interval milliseconds between attempts.
  • Reject with an Error whose message is Polling timed out once timeout has elapsed.

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.