expand
signature: expand(project: function, concurrent: number, scheduler: Scheduler): Observable
Recursively call provided function.
Examples
Example 1: Add one for each invocation
( StackBlitz |
jsBin |
jsFiddle )
import { interval } from 'rxjs/observable/interval';
import { of } from 'rxjs/observable/of';
import { expand, take } from 'rxjs/operators';
//emit 2
const source = of(2);
const example = source.pipe(
//recursively call supplied function
expand(val => {
//2,3,4,5,6
console.log(`Passed value: ${val}`);
//3,4,5,6
return of(1 + val);
}),
//call 5 times
take(5)
);
/*
"RESULT: 2"
"Passed value: 2"
"RESULT: 3"
"Passed value: 3"
"RESULT: 4"
"Passed value: 4"
"RESULT: 5"
"Passed value: 5"
"RESULT: 6"
"Passed value: 6"
*/
//output: 2,3,4,5,6
const subscribe = example.subscribe(val => console.log(`RESULT: ${val}`));
Related Recipes
Additional Resources
- expand
- Official docs
Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/expand.ts