BACK
PracticeReal-life: forkJoin() with a per-field fallback
PRACTICE · 65 · 練

Real-life: forkJoin() with a per-field fallback

One request fails. Catch the error inside the profile$ field so forkJoin still completes.

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

const user$ = of('profile');
const profile$ = throwError(() => new Error('missing')).pipe(
  catchError(() => of('guest'))
);

const result$ = forkJoin({ user: user$, profile: profile$ }).pipe(
  map(({ user, profile }) => user + ':' + profile)
);

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