New to transformation operators? Check out the article Get started transforming streams with map, pluck, and mapTo!
ββββ
Example 1: Map every emission to string
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+import { interval } from 'rxjs';import { mapTo } from 'rxjs/operators';β//emit value every two secondsconst source = interval(2000);//map all emissions to one valueconst example = source.pipe(mapTo('HELLO WORLD!'));//output: 'HELLO WORLD!'...'HELLO WORLD!'...'HELLO WORLD!'...const subscribe = example.subscribe(val => console.log(val));
Example 2: Mapping clicks to string
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+import { fromEvent } from 'rxjs';import { mapTo } from 'rxjs/operators';β//emit every click on documentconst source = fromEvent(document, 'click');//map all emissions to one valueconst example = source.pipe(mapTo('GOODBYE WORLD!'));//output: (click)'GOODBYE WORLD!'...const subscribe = example.subscribe(val => console.log(val));
βHTTP Pollingβ
βSave Indicatorβ
βSmart Counterβ
βStop Watchβ
βmapTo π° - Official docs
βChanging behavior with mapToβ
π₯ π΅ - John Linquist
βTransformation operator: map and mapToβ
π₯ π΅ - AndrΓ© Staltz
π Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/mapTo.tsβ