Error handling in flyd (Proposal)
See original GitHub issueI want to propose we implement the bifunctor spec to facilitate error handling.
If we look at the synchronisation quadrant
Flyd belongs in the asynchronous multi-value category (observable).
Looking at the Single value asynchronous landscape in functional fantasy-land compliant javascript one of the outstanding libraries is Fluture
They implement Bifunctor, Monad and ChainRec.
I would like to propose that flyd does the same.
This will allow users of flyd to operate with the same interface on single-value and multi-value async data.
There are two methods of note WRT error handling:
bimap :: (Bifunctor f) => f a b ~> (a -> c) -> (b -> d) -> f c d
fold :: (Bifunctor f) => f a b ~> (a -> c) -> (b -> c) -> f c d
A contrived example using Fluture
const example = Future.encaseP(fetch)('/example.json')
.chain(res => future.encaseP(()=> res.json())())
.bimap(tap(success => console.log(success)), tap(error => console.error(error)))
.fold(Either.Right, Either.Left)
equivalent code in flyd if we go this route:
const example = flyd.fromPromise(fetch('./example.json'))
.chain(res => flyd.fromPromise(res.json()))
.bimap(tap(success => console.log(success)), tap(error => console.error(error)))
.fold(Either.Right, Either.Left)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
I’ve tested this out a bit using my own codebase, but rather than making flyd a bifunctor I’ve just been hosting an either inside the stream
This has actually been quite sufficient for my error handling use cases.
I’m closing this issue because of that.
Hey @StreetStrider
I think we can agree on a few things.
The question is then how do we solve both.
My thesis in this issue was that we could make Flyd a bifunctor to better model the same flows we see in libraries like Fluture.
However, I also really like composing smaller things together to make larger things. I think if we can get away with just supporting those two use cases (not swallowing errors, and not breaking on errors) we allow the end user to pick their error handling strategy. An example would be using
Either
.e.g. imagine:
I imagine that it’s much superior for the user to be able to do something like:
A solution like the above
But that is predicated on us not violating the two use cases.
You could also implement something like:
if you want your stream to end on errors.
With the example above I think we might want to allow values other than
true
in end streams.