Fix async race in `fetchWithCache`

hard
By FrontendAtlas Editorial · Updated Oct 14, 2026
A teammate built a shared fetch helper to avoid duplicate network work, but real traffic exposed two nasty bugs. Concurrent callers with the same key still fan out into multiple requests, and a failed request can leave the helper stuck in a bad in-flight state. Fix it so callers share one active request per key, successful responses cache correctly, and failures can retry cleanly.

Solution idea

const cache = new Map();

const inflight = new Map();

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.