import { of, from, fromEvent } from 'rxjs';// of - emit static valuesof(1, 2, 3).subscribe(val => console.log(val)); // 1, 2, 3// from - convert array to observablefrom([10, 20, 30]).subscribe(val => console.log(val)); // 10, 20, 30// from - convert promise to observablefrom(fetch('/api/data')).subscribe(response => console.log(response));// fromEvent - DOM event to observableconst clicks$ = fromEvent(document, 'click');clicks$.subscribe(event => console.log('Clicked at:', event.clientX, event.clientY));
2. interval, timer, and range Observable Creators
Operator
Syntax
Description
Emission Pattern
interval
interval(period)
Emits sequential numbers at specified interval (ms), starting from 0
0, 1, 2, 3... (every period ms)
timer
timer(delay, period?)
Emits after delay, then optionally repeats at interval. Single emission if no period
Initial delay, then interval emissions
range
range(start, count?)
Emits sequence of numbers synchronously from start to start+count-1
Synchronous number sequence
Example: Time-based and sequence creators
import { interval, timer, range } from 'rxjs';import { take } from 'rxjs/operators';// interval - emit every 1000msinterval(1000).pipe(take(3)) .subscribe(n => console.log('Interval:', n)); // 0, 1, 2// timer - emit after 2s, then every 1stimer(2000, 1000).pipe(take(3)) .subscribe(n => console.log('Timer:', n)); // 0 (after 2s), 1, 2// timer - single emission after delaytimer(3000).subscribe(() => console.log('Delayed action'));// range - synchronous sequencerange(1, 5).subscribe(n => console.log('Range:', n)); // 1, 2, 3, 4, 5
3. fromPromise and async Observable Integration
Method
Syntax
Description
Behavior
from(promise)
from(promiseObj)
Converts Promise to Observable - emits resolved value or error
Single emission on resolve, error on reject
defer
defer(() => promise)
Creates Observable factory - Promise created per subscription (lazy)
Fresh Promise instance for each subscriber
async/await
await firstValueFrom(obs$)
Convert Observable to Promise for async/await syntax
Waits for first emission, then resolves
Example: Promise and async integration
import { from, defer } from 'rxjs';import { firstValueFrom } from 'rxjs';// Convert Promise to Observableconst promise = fetch('/api/user');from(promise).subscribe( response => console.log('Success:', response), error => console.error('Error:', error));// Lazy Promise creation with deferconst lazyPromise$ = defer(() => fetch('/api/data'));lazyPromise$.subscribe(); // Fetch happens only on subscribe// Convert Observable to Promise for async/awaitasync function fetchData() { const obs$ = from(fetch('/api/items')); const response = await firstValueFrom(obs$); return response.json();}
Note: Use defer when you need fresh Promise instances for each subscription.
Direct from(promise) shares the same Promise result across all subscribers.
4. ajax and fetch Observable Wrappers
Function
Syntax
Description
Features
ajax
ajax(urlOrRequest)
RxJS XMLHttpRequest wrapper with built-in operators support