kefir: Exception handling in .map, .flatMap and other operations

Should Kefir.Stream emit a error in case of thrown exception in .map()?

For example, the following code will fail with an uncaught exception:

function f(x) {
  if (x % 2 === 0) {
    throw Error();
  } else {
    return x;
  }
}
const stream = Kefir.sequentially(1000, [1, 2, 3]);
const mapped = stream.map(f);
mapped.log();

There is a non-obvious way to prevent this by wrapping map callback into try-catch inside flatMap:

function f(x) {
  if (x % 2 === 0) {
    throw Error();
  } else {
    return x;
  }
}
const stream = Kefir.sequentially(1000, [1, 2, 3]);
const mapped = stream.flatMap(function (value) {
  try { return Kefir.constant(f(value)); }
  catch (error) { return Kefir.constantError(error); }
});
mapped.log();

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Since this is foundational to the way Kefir was designed, this is unlikely to change, so I’m going to close this issue.