This works fine:
var Bacon = require('baconjs');
var a = Bacon.fromArray([1,2,3])
a.onValue(function(val){
console.log(val)
})
a.onEnd(function(){
console.log("END")
})
... meaning that onValue and onEnd got called properly and this is the result:
1
2
3
END
This doesn't work as (at least I) expected:
var Bacon = require('baconjs');
var bus = new Bacon.Bus();
var a = bus.flatMap(function(){
return Bacon.fromArray([1,2,3])
})
a.onValue(function(val){
console.log(val)
})
a.onEnd(function(){
console.log("END")
})
bus.push(1)
... meaning that in this case 'onEnd' didn't get called, with the result just being:
1
2
3
Is there a reason and what would be the solution ?
UPDATE
Here's exactly what I needed in the end:
var Bacon = require('baconjs');
var a = Bacon.fromArray([1,2,3])
.fold(0,function(a,b){return a+b;})
.filter(function(val){ return val > 0 });
var bus = new Bacon.Bus();
var ff = bus.flatMap(function(){
return Bacon.combineTemplate({status:true, result:a});
});
ff.onValue(function(val){
console.log(val)
setImmediate(function(){
bus.push(3)
})
})
ff.onEnd(function(){
console.log("END")
})
bus.push(3)
Basically I needed to know when the bus ended so that I could ask the database for another chunk of records, and so on. The problem is that without an 'end' signal on the bus that wouldn't work. And although something like this works:
var Bacon = require('baconjs');
var bus = new Bacon.Bus();
bus.onValue(function(){
var a = Bacon.fromArray([1,2,3])
a.onValue(function(val){
console.log(val)
})
a.onEnd(function(){
console.log("END")
})
})
bus.push(1);
The fact that there is a callback there was annoying me to no end. So the actual solution above is quite nice
That would be because the Bus doesn't end: you can still push more events to the Bus. The only case when a Bus ends is when you call the end() method on the Bus.