Back to results for “HTTP Client & Interceptors”
Angular › RxJS & Observables

What is an observable in RxJS, and how is it different from a promise?

Beginner

Answer

An observable represents a stream of values that can arrive over time, and you subscribe to it to receive those values as they happen. Unlike a promise, which resolves with exactly one value a single time, an observable can emit zero, one, or many values over its lifetime, and it can also be cancelled by unsubscribing.
import { of } from 'rxjs';

const numbers$ = of(1, 2, 3);

numbers$.subscribe((value) => console.log(value));
// Logs 1, 2, then 3
Real-world example A search feature uses an observable to represent a stream of user keystrokes over time, something a single promise could not naturally represent, since a promise only ever resolves once with one final value.

Common follow-ups: Why does an observable need to be subscribed to before it starts emitting values?;What happens to an observable's values if nothing ever subscribes to it?