Skip to curriculum
Jump to the editor

14.11. Share one request between identical callers

Medium
Topics
20–30 min

On 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

JAVASCRIPT
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 of fn(key)'s result, shared with every other caller of that key while it runs.

Requirements

  1. The first call for a key calls fn(key) at once.
  2. While that request runs, every call with the same key returns the same promise object, and fn is not called again, however late the caller comes.
  3. Different keys get separate requests. Keys compare the way a Map compares them: 1 and "1" differ, NaN matches NaN, objects match only themselves.
  4. A failure reaches every caller that shared the request, with fn's own error.
  5. Nothing is cached: once the request has settled, fulfilled or rejected, the next call for that key starts a new request.
  6. The entry is removed before callers' then callbacks run, so a caller that asks again when its answer arrives starts a new request.
  7. If fn throws, get returns a rejected promise (it never throws), and the next call tries again.
  8. Each function shareInflight returns keeps its own requests.

Examples

JAVASCRIPT
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 callers
JAVASCRIPT
await getUser("/api/me");  // request 1
await getUser("/api/me");  // request 2: the first had finished, nothing is cached
JAVASCRIPT
getUser("/api/team");      // its own request
getUser("/api/me");        // another one: different key

Environment

  • Plain JavaScript in a browser page; the tests call the global shareInflight from share-inflight.js.
  • The tests run on a virtual clock that replaces setTimeout. The page has no network: a test that uses fetch puts 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

  1. What has to be stored per key, and when does it stop being true that a request is running?
  2. A then callback 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
Back to the question
Loading the editor…