SwitchMap
Projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recently projected Observable.
switchMap<T, R, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, ObservedValueOf<O> | R>
Parameters | Description |
---|---|
project | A function that, when applied to an item emitted by the source Observable, returns an Observable. |
resultSelector optional | Optional. Default is undefined. Type: (outerValue: T, innerValue: ObservedValueOf, outerIndex: number, innerIndex: number) => R. |
OperatorFunction<T, ObservedValueOf
Generate new Observable according to source Observable values
import { of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
const switched = of(1, 2, 3).pipe(switchMap((x: number) => of(x, x ** 2, x ** 3)));
switched.subscribe(x => console.log(x));
// outputs
// 1
// 1
// 1
// 2
// 4
// 8
// ... and so on