Which of the following is the correct syntax to create a simple observable in Angular?
easy📝 Syntax Q3 of 15
Angular - RxJS and Observables Fundamentals
Which of the following is the correct syntax to create a simple observable in Angular?
Aconst obs = Observable.create('Hello');
Bconst obs = new Observable(subscriber => { subscriber.next('Hello'); subscriber.complete(); });
Cconst obs = new Promise(subscriber => { subscriber.next('Hello'); });
Dconst obs = Observable.emit('Hello');
Step-by-Step Solution
Solution:
Step 1: Recall the Observable constructor syntax
Observables are created with new Observable and a function receiving a subscriber object.
Step 2: Check each option for correct syntax
const obs = new Observable(subscriber => { subscriber.next('Hello'); subscriber.complete(); }); uses new Observable with subscriber.next and subscriber.complete correctly; others misuse methods or types.
Final Answer:
const obs = new Observable(subscriber => { subscriber.next('Hello'); subscriber.complete(); }); -> Option B
Quick Check:
Correct Observable syntax = B [OK]
Quick Trick:Use new Observable(subscriber => {...}) to create observables. [OK]
Common Mistakes:
MISTAKES
Using Promise instead of Observable
Calling non-existent Observable methods
Missing subscriber.next or complete calls
Master "RxJS and Observables Fundamentals" in Angular
9 interactive learning modes - each teaches the same concept differently