Angular › RxJS & Observables
What is an observable in RxJS, and how is it different from a promise?
BeginnerAnswer
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 3Real-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.