The Observable emits two values: 1 and 2, then completes.
Step 2: Understand subscription behavior
The subscriber logs each emitted value, so both 1 and 2 are printed.
Final Answer:
1 2 -> Option A
Quick Check:
Observable emits multiple values, all logged [OK]
Hint: Observable emits all next() values until complete [OK]
Common Mistakes:
Assuming only first value is logged
Thinking complete() emits a value
Confusing Observable with Promise single value
4. What is wrong with this Promise usage in Angular?
const p = new Promise((resolve, reject) => {
setTimeout(() => resolve('done'), 1000);
});
p.then(result => console.log(result));
p.catch(error => console.error(error));
p.cancel();
medium
A. Promises do not have a cancel() method, so calling it causes an error.
B. The Promise should use reject() instead of resolve() inside setTimeout.
C. The then() method should come after catch(), not before.
D. The Promise constructor is missing the reject parameter.
Solution
Step 1: Check Promise API for cancellation
Promises do not support cancellation; there is no cancel() method.
Step 2: Identify error cause
Calling p.cancel() will cause a runtime error because cancel() is undefined.
Final Answer:
Promises do not have a cancel() method, so calling it causes an error. -> Option A
Quick Check:
Promise cancellation is not supported [OK]
Hint: Promises cannot be cancelled; no cancel() method exists [OK]
Common Mistakes:
Thinking Promise supports cancel() like Observable
Confusing resolve and reject usage
Believing then() must come after catch()
5. You want to fetch user data that updates every 5 seconds and be able to stop the updates when the user navigates away. Which approach fits best in Angular?
hard
A. Use an Observable but do not unsubscribe; it stops automatically.
B. Use a Promise that resolves with user data every 5 seconds.
C. Use a Promise and call cancel() to stop fetching data.
D. Use an Observable that emits new user data every 5 seconds and unsubscribe on navigation.
Solution
Step 1: Identify need for multiple values over time
Fetching user data every 5 seconds means multiple values over time, which suits an Observable.
Step 2: Understand cancellation requirement
Observables can be unsubscribed to stop receiving updates, fitting the need to stop on navigation.
Final Answer:
Use an Observable that emits new user data every 5 seconds and unsubscribe on navigation. -> Option D
Quick Check:
Multiple updates + cancel = Observable with unsubscribe [OK]
Hint: Multiple updates + cancel = Observable with unsubscribe [OK]
Common Mistakes:
Using Promise for multiple repeated values
Assuming Promise supports cancel()
Not unsubscribing Observable causing memory leaks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.