nodeschool / discussions

:school::speech_balloon: need help with nodeschool? or just wanna ask a question? open an issue on this repo!
489 stars 107 forks source link

http colllect: using Bacon.js #1778

Closed nogo10 closed 8 years ago

nogo10 commented 8 years ago

It might be somewhat overkill but Im trying to use Bacon.js FRP to act as buffer/ stream processor for the http req response.. It works.. being able to log sequentially the characters. Problem is when I use proven Bacon.js fold() function to concat the resulting stream it logs a empty string..

Is the problem linked to learnyoumode testing framework? Is the testing framework capable of handling other buffer libs other than bl and concat?

 // copy your error output here

My Code

var http = require("http");
var bacon = require("baconjs").Bacon;
var count = 0;
var the_string = "";
var url = "http://baconjs.github.io/api.html" ;//process.argv[2];
var read = bacon.fromBinder(function(sink) {
 http.get(url, function(res) {
  res.setEncoding('utf8');
  res.on('data', function(chunk) {
   sink(chunk);
  });
 });
});

var concatenated = read.fold("", function(a,b) {a+b;
});

concatenated.log("concatenated");
omahlama commented 8 years ago

The problem you have is that the stream you create with fromBinder never ends. That means that the concatenated stream never gets a value. You should add:

res.on('end', function() {
  sink(new Bacon.End())
});
nogo10 commented 8 years ago

The unit test returns
"concatenated undefined" Somehow the data is NOT getting through..

to check I ran the onValue() The unit test returns: Trace outputs sucessive lines each with new char until full line of data is logged: "ipsum bla...bla"

nogo10 commented 8 years ago

Bacon.js can be a great buffer: Yes, I was missing res.on('end', function() { sink(new Bacon.End()) }); and also I was missing the return in the statement var concatenated = read.fold("", function(a,b) { return a+b; });