I'm studying RXJS and stuck with the problem: the same code with operators "reduce" and "scan" works in different ways, but I think that must return equal result. Example below. Please help.
const txtElement1 = document.getElementById('txt1');
const txtElement2 = document.getElementById('txt2');
const txtElement3 = document.getElementById('txt3');
// function return Observable
function get(array, initValue) {
return Rx.Observable.create(observer => {
let timer = initValue;
array.forEach(item => {
setTimeout(() => observer.next(item), timer);
timer += 1000;
});
});
}
// 1) don't work with "reduce"
var stream1$ = get(['John', 'Ann', 'Bob'])
.reduce(function(acc, x) {
return acc + ` ${x}`;
}, 'first - ');
stream1$.subscribe(text => txtElement1.innerHTML = text);
// 2) the same code, but with "scan" - working
var stream2$ = get(['John', 'Ann', 'Bob'])
.scan(function(acc, x) {
return acc + ` ${x}`;
}, 'second - ');
stream2$.subscribe(text => txtElement2.innerHTML = text);
// 3) and the simple Observable with "reduce" - working
var stream3$ = Rx.Observable.from(['John', 'Ann', 'Bob'])
.reduce(function(acc, x) {
return acc + ` ${x}`;
}, 'third - ');
stream3$.subscribe(text => txtElement3.innerHTML = text);


