Search code examples
javascriptbacon.js

Bacon.js accumulator


my_Stream is data I want to accumulate and assign to variable for further processing. My question: How do I get the contents of the variable the_string to console.log ONCE THE STREAM IS FINISHED?

my_Stream.onValue(function(value) {
 the_string = the_string.concat(value);
});

My full code can be found in github issue page: github.com/nodeschool/discussions/issues/1778


Solution

  • What you want to use is fold, which scans the stream and outputs a value only when the stream ends.

    const stream = Bacon.sequentially(100, ["Hello", "world"])    
    stream.log("stream")
    const concatenated = stream.fold("", (a,b) => a + b)
    concatenated.log("concatenated")
    

    http://jsbin.com/yodudiqovi/edit?html,js,console