Manage State in RxJS with StartWith and Scan

InstructorJohn Lindquist

Share this video with your friends

Send Tweet

The scan operator in RxJS is the main key to managing values and states in your stream. Scan behaves just as a reduce function would, but scan is able to collect values from streams over time. This lesson covers using startWith to set the initial accumulator value then using scan to update the value of the clock from the clicks and interval.

Fabio Biondi
~ 8 years ago

Here my quick fix to the example:

.startWith(new Date().toString())

and

.scan((acc, action) => {
     let date = new Date(acc);
     switch (action) {
        case 'xyz':
           date.setSeconds(date.getSeconds() + 1);
           ...
     }
     return date;
     
Thein
~ 6 years ago

can I pass initial new date to scan function without using startWith.