Single Flight Dedupe Concurrent Calls
Deduplicate concurrent identical calls to an async function so only one execution runs and all callers share its result.
Code
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;}What the code does
Single Flight Dedupe Concurrent Calls keeps everything in the functions singleFlight and d.
Await points sequence the asynchronous work; a rejection lands at the awaiting line.
Using it in your own code
The main signature reads export function singleFlight(fn)
singleFlight is what another module would consume; nothing else here is load-bearing for callers.
Interactive Tools for This Topic
Duplicate Line RemoverFind unique, duplicate, or counted lines with optional case sensitivity and whitespace trimming, all processed locally in the browser.Cache-Control BuilderCompose a valid Cache-Control header from directives such as no-store, private, max-age, and immutable.HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.
FAQ
- Are there any imports to resolve outside the language?
- No. Every import resolves against the language's own modules. The file defines `singleFlight`, `d` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.