Skip to content

Teach RxJS with retrying requests - #37

Merged
gpbsumido merged 4 commits into
ps/ng-05-difrom
ps/ng-06-rxjs
Nov 21, 2026
Merged

gpbsumido merged 4 commits into
ps/ng-05-difrom
ps/ng-06-rxjs

Conversation

@gpbsumido

@gpbsumido gpbsumido commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

Sixth lesson in my Angular guide: RxJS and HTTP. The feature slice is retrying requests that fail for a moment.

What shipped

retryTransientErrors interceptor

  • A functional interceptor, registered with withInterceptors() in app.config.ts.
  • It retries a GET that fails with a network error (status 0) or a 5xx, twice, with exponential backoff: 400ms, then 800ms.
  • A 404 fails straight away, and only GETs are retried at all.
  • Before this, one network blip put the Projects app straight into its error state.

Lesson 06: RxJS and HTTP (/thoughts/angular-06-rxjs)

  • Covers the material at three levels (simple, intermediate, expert), then pitfalls and interview Q&A:
    • cold HTTP Observables
    • toSignal vs async
    • a table of the four higher-order mapping operators
    • interceptors
    • hot vs cold and Subjects
    • shareReplay with refCount
    • marble testing
  • The live demo puts two search boxes over a deliberately slow fake server. Typing "sig" leaves mergeMap on the stale "s" answer, while switchMap shows "sig".
  • searchAsYouType is the small operator behind the demo, with marble tests.

Version and changelog. 1.4.5, with a matching changelog entry.

Bundle

Initial bundle went from 384.98 kB to 389.82 kB. I measured where the 4.8 kB comes from:

Build Initial bundle
This branch 389.82 kB
Without the interceptor 389.00 kB
Without lesson 06 registered 388.39 kB

RxJS's timer and async-scheduler code is shared by the interceptor's backoff and the lesson's delay, so it ends up in an eager chunk either way. Swapping delay for timer saved only 0.26 kB, so I kept the readable version. Headroom under the 400 kB budget is now 10 kB.

Commits

  1. Add failing specs for retrying transient errors and lesson 06
  2. Retry transient GET failures with backoff
  3. Write lesson 06 with a switchMap vs mergeMap race
  4. Bump to 1.4.5 with the lesson 06 changelog entry

Verification

  • RED first. The RED commit fails to compile, because the interceptor, the operator and the lesson don't exist yet.
  • Registration check. With the interceptor un-registered, the app-config spec fails. That spec retries a GitHub GET through the real appConfig providers.
  • Tests. ng test: 54 files, 387 tests, exit 0. They include:
    • interceptor specs on HttpTestingController with fake timers: 5xx, status 0, the backoff schedule and giving up, 404, POST
    • two marble tests
    • the demo race under fake timers
  • Static checks. Lint and both tsc --noEmit configs are clean.
  • Production build. Exit 0, no budget warnings. Prerendered lesson 06 shows "Part 6 of 6 · 0 of 6 complete".

TO-DOs for this PR

  • Before/after screenshots of the lesson 06 race demo after typing "sig", light and dark. Chromium couldn't launch in the environment I built this in. The interceptor has nothing visible to shoot.

@gpbsumido
gpbsumido marked this pull request as ready for review November 21, 2026 15:11
@gpbsumido
gpbsumido added this pull request to stack #46 November 21, 2026 17:00
@gpbsumido
gpbsumido merged commit 530c68f into develop Nov 21, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant