0Pricing
Angular Academy · Lesson

Retry and Loading Indicators

Implement retries and global spinners.

Retry and Loading Indicators is a free Angular Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two cross-cutting concerns

Interceptors are perfect for two app-wide behaviors: automatically retrying failed requests, and tracking a global loading state while any request is in flight.

Retrying with retry

Pipe retry after next(req) to re-attempt transient failures.

import { retry } from 'rxjs';

export const retryInterceptor: HttpInterceptorFn = (req, next) =>
  next(req).pipe(retry(2));

Retry only safe requests

Retry idempotent methods (GET, HEAD) freely; be careful retrying POST/PUT which may cause duplicates. Guard by method.

export const retryInterceptor: HttpInterceptorFn = (req, next) => {
  if (req.method !== 'GET') return next(req);
  return next(req).pipe(retry(2));
};

Retry with delay and backoff

Add a delay so retries do not hammer the server, with exponential backoff for resilience.

import { timer } from 'rxjs';

next(req).pipe(
  retry({
    count: 3,
    delay: (_err, n) => timer(Math.pow(2, n) * 500)
  })
);

Conditional retry

Only retry on retryable errors (5xx, network), not on 4xx client errors which will fail again.

retry({
  count: 2,
  delay: (err, n) =>
    err.status >= 500 ? timer(n * 500) : throwError(() => err)
})

Global loading state

A loading service tracks the count of active requests so the UI can show a global spinner.

export class LoadingService {
  private active = signal(0);
  readonly isLoading = computed(() => this.active() > 0);
  start() { this.active.update(n => n + 1); }
  stop()  { this.active.update(n => Math.max(0, n - 1)); }
}

Loading interceptor

Increment on request start, decrement on finalize (success or error) using finalize.

import { finalize } from 'rxjs';

export const loadingInterceptor: HttpInterceptorFn = (req, next) => {
  const loading = inject(LoadingService);
  loading.start();
  return next(req).pipe(finalize(() => loading.stop()));
};

Why finalize

finalize runs on completion, error, OR unsubscription, guaranteeing the loading counter is always decremented — no stuck spinner even if the request fails or is cancelled.

Showing the spinner

Bind the loading signal in your root template.

// root component template:
// @if (loading.isLoading()) {
//   <app-spinner />
// }

Excluding background requests

Some polling or prefetch requests should not trigger the spinner. Use a custom header or context token to skip them.

import { HttpContextToken } from '@angular/common/http';
export const SKIP_LOADING = new HttpContextToken(() => false);
// in interceptor: if (req.context.get(SKIP_LOADING)) return next(req);

Combining retry and loading

Order them so retries happen within a single loading span: start loading, retry internally, finalize once. Place loading outermost so it covers all retry attempts.

Quick Check

Test your understanding of retry and loading indicators.

Recap: Retry & Loading Indicators

Interceptors handle retries and global loading cleanly.

  • retry with delay/backoff; restrict to idempotent or retryable cases.
  • Track active requests in a service; finalize to decrement.
  • Use context tokens to skip background requests.

You have completed HttpClient Interceptors.

Frequently asked questions

Is the “Retry and Loading Indicators” lesson free?

Yes — the full text of “Retry and Loading Indicators” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

What will I learn in “Retry and Loading Indicators”?

Implement retries and global spinners. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Retry and Loading Indicators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Functional Interceptors
  2. Adding Auth Headers
  3. Handling Errors Globally
  4. Retry and Loading Indicators
← Back to Angular Academy