Fix async race in `fetchWithCache`
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.
- Aced: ElevenLabs senior full-stack (frontend-leaning) interview experience, 2026
One senior account describes a live round spent fixing a practical problem together with the interviewer. This async-race fix is a practice version of that round shape, not the reported problem.
Solution
Overview
const cache = new Map();
const inflight = new Map();
export default async function fetchWithCache(key, fetcher){
if(cache.has(key)) return cache.get(key);
if(inflight.has(key)) return inflight.get(key);
const p = Promise.resolve()
.then(() => fetcher())
.then(res => {
cache.set(key, res);
return res;
})
.finally(() => {
inflight.delete(key);
});
inflight.set(key, p);
return p;
}
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.