0
0
Angularframework~10 mins

Subscribing to observables in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subscribing to observables
Create Observable
Subscribe to Observable
Observable emits value
Subscriber callback runs
Process emitted value
Observable completes or errors
Subscription ends
This flow shows how an observable is created, subscribed to, emits values, and how the subscriber processes those values until completion or error.
Execution Sample
Angular
import { of } from 'rxjs';

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

obs$.subscribe(value => {
  console.log('Received:', value);
});
This code creates an observable that emits 1, 2, and 3, then subscribes to it to log each emitted value.
Execution Table
StepActionObservable StateSubscriber CallbackOutput
1Create observable with values 1, 2, 3Ready to emitNo callback yet
2Subscribe to observableWaiting to emitSubscriber registered
3Observable emits 1Emitting first valueCallback runs with value=1Received: 1
4Observable emits 2Emitting second valueCallback runs with value=2Received: 2
5Observable emits 3Emitting third valueCallback runs with value=3Received: 3
6Observable completesNo more valuesNo callback
7Subscription endsCompletedNo callback
💡 Observable completes after emitting all values, subscription ends.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
valueundefined1233
obs$Observable createdEmitting 1Emitting 2Emitting 3Completed
Key Moments - 3 Insights
Why does the subscriber callback run multiple times?
Because the observable emits multiple values (1, 2, 3), the subscriber callback runs once for each emitted value as shown in steps 3, 4, and 5 in the execution table.
What happens after the observable completes?
After the observable completes (step 6), no more values are emitted and the subscription ends (step 7), so the subscriber callback does not run anymore.
Is the subscription active before subscribing?
No, the subscription only becomes active after calling subscribe (step 2). Before that, the observable is just created but not emitting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'value' variable after step 4?
A3
B1
C2
Dundefined
💡 Hint
Check the 'variable_tracker' table column 'After Step 4' for 'value'
At which step does the observable complete?
AStep 5
BStep 6
CStep 7
DStep 3
💡 Hint
Look at the 'execution_table' row where 'Observable completes' is mentioned
If the observable emitted only one value, how many times would the subscriber callback run?
AOnce
BTwice
CThree times
DNever
💡 Hint
Subscriber callback runs once per emitted value as shown in steps 3-5
Concept Snapshot
Subscribing to observables:
- Create observable (e.g., of(1,2,3))
- Call subscribe() to listen
- Callback runs for each emitted value
- Observable can complete or error
- Subscription ends after completion or error
Full Transcript
This visual execution shows how subscribing to an observable works in Angular. First, an observable is created with values 1, 2, and 3. Then, subscribing to it registers a callback. When the observable emits each value, the callback runs and processes it. After all values are emitted, the observable completes and the subscription ends. Variables like 'value' track emitted data step-by-step. Key moments clarify why callbacks run multiple times and what happens after completion. Quiz questions test understanding of these steps.