function stable operator
Recursively projects each source value to an Observable which is merged in the output Observable.
expand(project: (value: T, index: number) => O, concurrent?: number, scheduler?: SchedulerLike): OperatorFunction<T, ObservedValueOf<O>> project | (value: T, index: number) => O | |
concurrent | number | Optional. Default is |
scheduler | SchedulerLike | Optional. Default is |
OperatorFunction<T, ObservedValueOf<O>>
expand(project: (value: T, index: number) => O, concurrent: number, scheduler: SchedulerLike): OperatorFunction<T, ObservedValueOf<O>> project | (value: T, index: number) => O | |
concurrent | number | |
scheduler | SchedulerLike |
OperatorFunction<T, ObservedValueOf<O>>
It's similar to mergeMap, but applies the projection function to every source value as well as every output value. It's recursive.
Returns an Observable that emits items based on applying a function that you supply to each item emitted by the source Observable, where that function returns an Observable, and then merging those resulting Observables and emitting the results of this merger. Expand will re-emit on the output Observable every source value. Then, each output value is given to the project function which returns an inner Observable to be merged on the output Observable. Those output values resulting from the projection are also given to the project function to produce new output values. This is how expand behaves recursively.
Start emitting the powers of two on every click, at most 10 of them
import { fromEvent, map, expand, of, delay, take } from 'rxjs';
const clicks = fromEvent(document, 'click');
const powersOfTwo = clicks.pipe(
map(() => 1),
expand(x => of(2 * x).pipe(delay(1000))),
take(10)
);
powersOfTwo.subscribe(x => console.log(x));
© 2015–2022 Google, Inc., Netflix, Inc., Microsoft Corp. and contributors.
Code licensed under an Apache-2.0 License. Documentation licensed under CC BY 4.0.
https://rxjs.dev/api/operators/expand