BACK
PracticeReal-life: shareReplay() HTTP cache
PRACTICE · 62 · 練

Real-life: shareReplay() HTTP cache

The API should be called once, and both subscribers should get the cached value.

  1. Solve the task in the editor — no long theory.
  2. Run the check and compare your output with the expected result.
  3. Stuck? Open the solution and carry the approach into your own code.
Solution spoiler · click to reveal
const { defer, of, shareReplay } = Rx;

let calls = 0;
const api$ = defer(() => {
  calls += 1;
  return of('api-' + calls);
}).pipe(
  shareReplay({ bufferSize: 1, refCount: false })
);

api$.subscribe(value => console.log('A:' + value));
api$.subscribe(value => console.log('B:' + value));
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...