Appearance
Flow Diagram
Appearance
Projects each source value to an inner Observable, cancelling any previously active inner Observable when a new source value arrives.
switchMap<T, R>(project: (value: T, index: number) => ObservableInput<R>): OperatorFunction<T, R>source: --a---------b---------|
switchMap(x => interval(1).take(3))
result: --a1-a2-(b1)-b2-b3---|
(a3 cancelled when b arrives)
import { fromEvent } from 'rxjs'
import { switchMap, debounceTime } from 'rxjs/operators'
import { ajax } from 'rxjs/ajax'
fromEvent(input, 'input').pipe(
debounceTime(300),
switchMap(e => ajax.getJSON(`/api/search?q=${e.target.value}`))
).subscribe(results => render(results))
// Previous request is cancelled when user types again