0

I am trying to produce the zip example from BaconJS. But it doesn't work.

var obs = Bacon.fromArray([{ x: 1, y: 2 },
                           { x: 3, y: 4 }]);

var x = obs.map('.x');
var y = obs.map('.y');

var result = x.zip(y, function(x, y) {
  return x + y;
});

// This doesn't work
// if `result` is replaced with `x` then it produces 1, 3 correctly
result.onValue(function(value) {
  $("#events").append($("<li>").text(value))
});

Example JSFiddle.

user3995789
  • 3,452
  • 1
  • 19
  • 35

1 Answers1

1

The problem is with Bacon.fromArray, which behaves differently (synchronously) than other streams. This is a typical problem that many people run into in example code. See the FAQ.

One way to solve this is to add .delay(0) to your stream, another is to use Bacon.sequentially.

I've updated your fiddle so it works.

OlliM
  • 7,023
  • 1
  • 36
  • 47