baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript
https://baconjs.github.io
MIT License
6.47k stars 331 forks source link

Can't map function to errors only so that they remain errors. #670

Open mrehayden1 opened 8 years ago

mrehayden1 commented 8 years ago

Could we have a method on Observable that will map over error values only but not feed them into the value stream?

I've needed this a number of times and come up with this extension using a modified version of mapError(), which I have called mapErrors().

Bacon.Observable.prototype.mapErrors = function(f) {
    return withDesc(new Bacon.Desc(this, "mapError", [f]), this.withHandler(function (event) {
        if (event.isError()) {
            return this.push(new Bacon.Error(f(event.error)));
        } else {
            return this.push(event);
        }
    }));
};

This can't use the function construction features because it's all hidden away in the Bacon module so it would great to see it included in the distribution.

raimohanska commented 8 years ago

There's indeed a flatMapError method available that you can use to this effect. Unfortunately it doesn't currently support function construction from arguments...

Maybe we should do 2 things:

1) introduce flatMapEvent, that would allow you to flatmap any event (value, error, end) into any sequence of events 2) publish the function construction API to make it usable for custom functions 3) fix flatMapError to support function construction