Bird
0
0

Identify the error in this Angular observable subscription:

medium📝 Debug Q6 of 15
Angular - RxJS and Observables Fundamentals
Identify the error in this Angular observable subscription:
const obs = new Observable(subscriber => { subscriber.next('data'); });
obs.subscribe(data => console.log(data));
obs.unsubscribe();
ASubscription must be inside subscribe callback.
BMissing subscriber.complete() call.
CObservable cannot emit string data.
DCalling unsubscribe on the observable instead of the subscription.
Step-by-Step Solution
Solution:
  1. Step 1: Understand unsubscribe usage

    unsubscribe() is a method on the subscription object returned by subscribe(), not on the observable.
  2. Step 2: Check the code

    The code calls obs.unsubscribe(), which is invalid and causes an error.
  3. Final Answer:

    Calling unsubscribe on the observable instead of the subscription. -> Option D
  4. Quick Check:

    Unsubscribe method location = D [OK]
Quick Trick: Call unsubscribe on subscription, not observable. [OK]
Common Mistakes:
MISTAKES
  • Calling unsubscribe on observable
  • Forgetting to store subscription
  • Confusing complete with unsubscribe

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes