BACK
PracticeReal-life: catchError() fallback cache
PRACTICE · 31 · 練

Real-life: catchError() fallback cache

If the API fails, return cached instead of the error.

  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 { throwError, of, catchError } = Rx;

const api$ = throwError(() => new Error('offline'));

const result$ = api$.pipe(
  catchError(() => of('cached'))
);

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