import { Component, OnInit } from '@angular/core'; import { Observable } from 'rxjs'; @Component({ selector: 'app-test', template: `<p>Check console output</p>` }) export class TestComponent implements OnInit { data$: Observable<number> = new Observable(observer => { observer.next(1); observer.next(2); observer.complete(); }); ngOnInit() { this.data$.subscribe(value => console.log(value)); } }
The observable emits two values, 1 and 2, before completing. The subscription logs each emitted value in order.
Option B uses the correct Observable constructor and calls observer.next and observer.complete properly.
Option B is invalid because 'of' is a standalone function, not a method on Observable.
Option B uses deprecated Observable.create and misses observer.complete.
Option B uses observer.emit which does not exist.
const obs$ = new Observable(observer => { setTimeout(() => { observer.next('data'); observer.complete(); }, 1000); }); obs$.subscribe(value => console.log(value));
The observable is cold, so the code inside runs when subscribed. The setTimeout delays emission by 1 second, then emits 'data' and completes. The subscription logs 'data' after 1 second.
const obs$ = new Observable(observer => { observer.next(10); observer.next(20); observer.next(30); observer.complete(); }); let received = []; obs$.subscribe(value => received.push(value));
The observable emits three values in order, so received has all three. After complete, the subscription is closed and no more values can be emitted.
Cold observables wait until someone subscribes before emitting data. Hot observables emit data independently of subscriptions, so subscribers may miss some values.