Learn RxJS
Search…
expand

signature: expand(project: function, concurrent: number, scheduler: Scheduler): Observable

Recursively call provided function.

Examples

Example 1: Add one for each invocation
1
// RxJS v6+
2
import { interval, of } from 'rxjs';
3
import { expand, take } from 'rxjs/operators';
4
5
//emit 2
6
const source = of(2);
7
const example = source.pipe(
8
//recursively call supplied function
9
expand(val => {
10
//2,3,4,5,6
11
console.log(`Passed value: ${val}`);
12
//3,4,5,6
13
return of(1 + val);
14
}),
15
//call 5 times
16
take(5)
17
);
18
/*
19
"RESULT: 2"
20
"Passed value: 2"
21
"RESULT: 3"
22
"Passed value: 3"
23
"RESULT: 4"
24
"Passed value: 4"
25
"RESULT: 5"
26
"Passed value: 5"
27
"RESULT: 6"
28
"Passed value: 6"
29
*/
30
//output: 2,3,4,5,6
31
const subscribe = example.subscribe(val => console.log(`RESULT: ${val}`));
Copied!

Additional Resources

  • expand 📰 - Official docs
Last modified 1yr ago