Skip to the page

Search, lists and data · mid

Polling with backoff

Polling chains setTimeout calls so requests never overlap, stops when a final state arrives, backs off exponentially (with a cap and jitter) after errors, pauses while the tab is hidden, aborts on unmount, and gives up gracefully after a limit.

The notes

What you must know

  • Chain setTimeout after each response finishes (not setInterval), so a slow request never overlaps the next.
  • Stop on a terminal state (payment 'paid' or 'failed', order 'delivered').
  • On errors: delay = min(cap, base × 2^attempt) with random jitter; reset to the base delay after a success; honour Retry-After on 429/503.
  • Pause on visibilitychange when hidden; poll immediately when visible again.
  • Abort the in-flight request and clear the timer on unmount (AbortController + clearTimeout).
  • Max attempts or a total time limit, then show 'Still checking… Retry' instead of spinning forever.
  • Prefer push (SSE/WebSocket) when available; Uber moved off polling because polls were most of its API traffic. For payments, Razorpay's docs say to verify on the server and use webhooks; the client polls your server for the order status.

The red pen

Common traps: mistake → fix

  • Mistake: setInterval(fetchStatus, 2000)

    Fix: overlapping requests on slow networks and out-of-order results.

  • Mistake: Retry immediately on error

    Fix: hammers a server that is already failing.

  • Mistake: Keep polling in a background tab

    Fix: wasted battery and quota.

  • Mistake: Treat a polling timeout as 'payment failed'

    Fix: it may still succeed; show 'pending'.

Easy to hard

Questions interviewers ask, easy to hard

  1. Show a payment status screen that waits for the result.
  2. Why not setInterval?
  3. The status API starts failing. What does your poller do?
  4. The user switches tabs for 10 minutes. Then?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview