14.11. Share one request between identical callers
Topics
This question is in
14. Timeouts, retries and concurrencyOn page load, the header, the sidebar and the profile card each ask for the current user. Three components, three identical requests, all in flight at the same moment. Sharing in-flight requests (also called request deduplication) fixes that without a cache: while a request for a key is running, every new caller for that key gets the same promise. Once it settles, the next caller starts a fresh request.
Write shareInflight(fn) in share-inflight.js. It returns get(key). While fn(key) is running, every call to get with that key returns the same promise; once it has settled, the entry is gone.
Signature
function shareInflight(fn) // returns get(key)fn(key): starts the work for a key. It may return a promise or a value, or throw.get(key): returns the promise offn(key)'s result, shared with every other caller of that key while it runs.
Requirements
- The first call for a key calls
fn(key)at once. - While that request runs, every call with the same key returns the same promise object, and
fnis not called again, however late the caller comes. - Different keys get separate requests. Keys compare the way a
Mapcompares them:1and"1"differ,NaNmatchesNaN, objects match only themselves. - A failure reaches every caller that shared the request, with
fn's own error. - Nothing is cached: once the request has settled, fulfilled or rejected, the next call for that key starts a new request.
- The entry is removed before callers'
thencallbacks run, so a caller that asks again when its answer arrives starts a new request. - If
fnthrows,getreturns a rejected promise (it never throws), and the next call tries again. - Each function
shareInflightreturns keeps its own requests.
Examples
const getUser = shareInflight((url) => fetch(url).then((response) => response.json()));
const a = getUser("/api/me");
const b = getUser("/api/me");
a === b; // true: one request, two callersawait getUser("/api/me"); // request 1
await getUser("/api/me"); // request 2: the first had finished, nothing is cachedgetUser("/api/team"); // its own request
getUser("/api/me"); // another one: different keyEnvironment
- Plain JavaScript in a browser page; the tests call the global
shareInflightfromshare-inflight.js. - The tests run on a virtual clock that replaces
setTimeout. The page has no network: a test that usesfetchputs in a fake one. - Keeping answers after they arrive (a cache with an expiry) and cancelling a shared request are not part of this problem.
Hints
- What has to be stored per key, and when does it stop being true that a request is running?
- A
thencallback on the promise you hand out runs when that promise settles. Which promise should you hand out so that your clean-up runs first?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it