Skip to the page

State, data and offline · senior

Network patterns: token refresh, batching, races and cancellation

Senior React Native coding rounds test async control: refresh an expired token exactly once while other requests wait, batch many small calls into one, cancel or ignore stale responses, debounce and rate-limit, all without leaking timers or listeners.

The notes

What you must know

  • Single-flight refresh: keep one in-flight refresh promise. Every 401 waits on it, then retries once. Skip the interceptor for the refresh endpoint itself; if refresh fails, log out.
  • Refresh token in Keychain/Keystore, access token in memory. Check expiry when the app comes back to the foreground.
  • Batching (a Zepto senior RN round): queue calls for a short window or until a max size, send one request, give each caller its own result, reject only the missing ones, and share one promise for a repeated id.
  • Races: when the response for "ab" arrives after "abc", the old result overwrites the new one. Fix with AbortController in the effect cleanup, or a request id / latest-query ref.
  • React 18 removed the "setState on an unmounted component" warning, but the stale-overwrite bug is still real.
  • Since 0.82, uncaught promise rejections are reported like uncaught errors (before, they were silently swallowed), so catch AbortError on purpose.
  • Debounce = wait until calls stop; throttle = at most once per interval; rate limit = at most N per window, queue the rest.

The red pen

Common traps: mistake → fix

  • Mistake: "Each failed request refreshes the token"

    Fix: with refresh-token rotation, the second refresh invalidates the first; use single-flight.

  • Mistake: "An isMounted flag fixes races"

    Fix: it hides the warning, not the stale overwrite; cancel or compare request ids.

  • Mistake: "Promise.all for a batch"

    Fix: one failure rejects every caller; settle each one separately.

Easy to hard

Questions interviewers ask, easy to hard

  1. Five requests get 401 at the same time. Implement refresh so it runs exactly once and all five retry.
  2. Write a batching function: calls within 50 ms become one API call, and each caller gets its own result.
  3. The user types fast and the "ab" response arrives after "abc". Fix it.
  4. Write rateLimiter(fn, limit) that queues calls over the limit instead of dropping them.

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