1

I've tried to find a good solution for adding errors to an bacon.js EventStream - and propagating them. All this because I wan't to handle the errors later possibly at multiple clients. I've found a hack with flatMap but it's a ... hack:

var streamWithPossibleProblems = bus.flatMap(function(v) {
    if (v == "problem") {
        return Bacon.sequentially(0, [new Bacon.Error("Error to be reported later")])
    }
    return v
});
flash42
  • 71
  • 6
  • Why the `Bacon.sequentially(0, `? – Bergi Nov 17 '14 at 17:04
  • I thought (mistakenly) that I need to return an EventStream here but it's enough to return a raw Bacon.Error it seems. – flash42 Nov 17 '14 at 22:16
  • 2
    Oh, that would be totally reasonable. But I wondered why you used `sequentially`, and sent a `0`? Just do `return Bacon.once(new Bacon.Error("fail"))`. – Bergi Nov 17 '14 at 22:23

1 Answers1

2

You can just return the Bacon.Error directly from flatMap:

var streamWithPossibleProblems = bus.flatMap(function(v) {
    if (v == "problem") {
        return new Bacon.Error("Error to be reported later")
    }
    return v
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
OlliM
  • 7,023
  • 1
  • 36
  • 47
  • It really is better and also mentioned in the bacon.js readme - this must be the canonical answer then. – flash42 Nov 17 '14 at 22:13