Single Flight Dedupe Concurrent Calls

Deduplicate concurrent identical calls to an async function so only one execution runs and all callers share its result.

JavascriptResilience

Code

snippet
export function singleFlight(fn){const inFlight=new Map();async function d(...a){const key=JSON.stringify(a);if(inFlight.has(key))return inFlight.get(key);const p=fn.apply(this,a).finally(()=>inFlight.delete(key));inFlight.set(key,p);return p;}d.inFlight=inFlight;return d;}