Academind - 3
Reduce vs Scan
by Suman Kumar
HTML
<div class="container">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.4.2/Rx.min.js"></script>
<!-- Reduce (like in normal Array function) will give final result and will not work till the Observable is closed-->
<!-- NOTE : Try running Reduce example without completing the Observable -->
<!-- Scan will give intermediate result and will work with the Observable still being active-->
JavaScript
var observer = {
next : function(val){
console.log(val);
},
error : function(err){
console.log(err);
}
}
var subj = new Rx.Subject();
subj.reduce((total, curVal) => total + curVal, 0)
.subscribe(observer);
subj.next(1);
subj.next(2);
subj.next(3);
setTimeout(() => {subj.next(10)}, 3000);
setTimeout(() => {subj.complete()}, 0);