githubEdit

scan

signature: scan(accumulator: function, seed: any): Observable

Reduce over time.


๐Ÿ’ก You can create Reduxarrow-up-right-like state management with scan!


Ultimate RxJSarrow-up-right

Examples

Example 1: Sum over time

( StackBlitzarrow-up-right )

// RxJS v6+
import { of } from 'rxjs';
import { scan } from 'rxjs/operators';

const source = of(1, 2, 3);
// basic scan example, sum over time starting with zero
const example = source.pipe(scan((acc, curr) => acc + curr, 0));
// log accumulated values
// output: 1,3,6
const subscribe = example.subscribe(val => console.log(val));

Example 2: Accumulating an object

( StackBlitzarrow-up-right | jsBinarrow-up-right | jsFiddlearrow-up-right )

Example 3: Emitting random values from the accumulated array.

( StackBlitzarrow-up-right )

Example 4: Accumulating http responses over time

( StackBlitzarrow-up-right )

Additional Resources


๐Ÿ“ Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/scan.tsarrow-up-right

Last updated