What is the difference between Observable and Promise in Angular?

What is the difference between Observable and Promise in Angular?

What is the difference between Observable and Promise in Angular?

Question

What is the difference between Observable and Promise in Angular?

Answer

Promise

  • Resolves once with a single value
  • Eager — starts executing immediately when created
  • Cannot be cancelled
  • No built-in operators for transformation
const promise = fetch('/api/users').then(r => r.json());
// Starts immediately, resolves once

Observable (RxJS)

  • Can emit zero, one, or many values over time
  • Lazy — nothing happens until you subscribe
  • Supports cancellation via unsubscribe
  • Composable with powerful operators (map, filter, switchMap, etc.)
import { Observable, interval } from 'rxjs';
import { map, take } from 'rxjs/operators';

// Emits 0,1,2,3,4 then completes
const five$ = interval(1000).pipe(take(5));

// Lazy — nothing happens yet
const sub = five$.subscribe(val => console.log(val));

// Cancel anytime
sub.unsubscribe();

In Angular HttpClient

// Returns Observable — subscribe to execute the request
this.http.get('/api/users').pipe(
  map(users => users.filter(u => u.active)),
  catchError(err => EMPTY),
).subscribe(users => this.users = users);

// Convert to Promise when needed
const users = await firstValueFrom(this.http.get('/api/users'));

When to Use Each

ScenarioUse
Single HTTP requestEither (Observable preferred in Angular)
Real-time/streaming dataObservable
Event streamsObservable
Simple async with async/awaitPromise (via firstValueFrom)
All Comments