Discover how observables make your app feel alive by reacting instantly to data changes!
Creating observables in Angular - Why You Should Know This
Imagine you want to watch for changes in a data source, like user input or server updates, and manually check for new values every second.
Manually checking for changes is slow, wastes resources, and can miss updates or cause bugs because you never know exactly when data changes.
Creating observables lets you listen to data streams that automatically notify you when new values arrive, so your app reacts instantly and efficiently.
setInterval(() => { if(dataChanged) { updateUI(); } }, 1000);const obs = new Observable(subscriber => { subscriber.next(data); }); obs.subscribe(value => updateUI(value));It enables building apps that respond smoothly and immediately to changing data without wasting effort or missing updates.
Think of a chat app where new messages appear instantly as they arrive, without you needing to refresh or wait.
Manual data checks are inefficient and error-prone.
Observables provide a clean way to watch and react to data changes.
This leads to faster, more responsive applications.
Practice
Solution
Step 1: Understand observable concept
Observables let you watch data or events and respond when they change.Step 2: Identify purpose in Angular
Angular uses observables to handle asynchronous data streams and events.Final Answer:
To watch and react to data or events over time -> Option BQuick Check:
Observable purpose = watch/react data [OK]
- Confusing observables with components
- Thinking observables style elements
- Mixing observables with routing
Solution
Step 1: Recall Observable creation syntax
Usenew Observable()with a function receiving a subscriber.Step 2: Check each option
Only const obs = new Observable(subscriber => { subscriber.next('Hello'); }); correctly usesnew Observable(subscriber => subscriber.next(...)).Final Answer:
const obs = new Observable(subscriber => { subscriber.next('Hello'); }); -> Option AQuick Check:
Correct syntax uses new Observable with subscriber function [OK]
- Using Observable.create which is deprecated
- Calling next on Observable directly
- Trying to call Observable as a function
const obs = new Observable(subscriber => { subscriber.next(1); subscriber.next(2); subscriber.complete(); subscriber.next(3); });
obs.subscribe(value => console.log(value));Solution
Step 1: Analyze subscriber.next calls
The observable sends 1, then 2, then calls complete(), then tries to send 3.Step 2: Understand complete behavior
After complete() is called, no more values are sent, so 3 is ignored.Final Answer:
1 2 -> Option AQuick Check:
complete stops emissions, so output = 1 2 [OK]
- Expecting values after complete()
- Ignoring complete() effect
- Thinking all next() calls always emit
const obs = new Observable(subscriber => { subscriber.next('Hi'); subscriber.error('Oops'); subscriber.next('Bye'); });Solution
Step 1: Review observable error handling
After calling subscriber.error(), the observable stops and no further next() calls run.Step 2: Check code sequence
Here, subscriber.next('Bye') is called after error, which is invalid and ignored.Final Answer:
Calling subscriber.next after subscriber.error -> Option DQuick Check:
next after error is invalid [OK]
- Thinking complete() is required after error()
- Believing next() after error() runs
- Confusing error() with complete()
Solution
Step 1: Understand timing requirements
We need to emit 1, 2, 3 spaced by 1 second each.Step 2: Analyze each option's timing
const obs = new Observable(subscriber => { [1,2,3].forEach(n => { setTimeout(() => subscriber.next(n), 1000); }); }); emits 1,2,3 all after 1 second simultaneously.
const obs = new Observable(subscriber => { for(let n=1; n<=3; n++) { setTimeout(() => subscriber.next(n), 1000); } }); emits all after 1 second simultaneously.
const obs = new Observable(subscriber => { let n=1; const interval = setInterval(() => { subscriber.next(n); if(n===3) clearInterval(interval); n++; }, 1000); }); uses setInterval to emit each number every 1 second and stops after 3.
const obs = new Observable(subscriber => { subscriber.next(1); subscriber.next(2); subscriber.next(3); }); emits all immediately without delay.Final Answer:
const obs = new Observable(subscriber => { let n=1; const interval = setInterval(() => { subscriber.next(n); if(n===3) clearInterval(interval); n++; }, 1000); }); -> Option CQuick Check:
setInterval with clearInterval emits spaced values [OK]
- Using forEach with setTimeout causes all to run at once
- Not clearing interval causing infinite emissions
- Emitting all values immediately without delay
Ready to prove it?
Pass the test and this pattern moves unknown
