Comments (6)
Looks pretty reasonable. I think Promise<R, E> → flyd$Stream<Either<R, E>>
is a straightforward conversion and your idea follows it. There was a couple of tickets here in tracker related to your topic. Looks like this is current state of the things to convert.
You've introduced pending state. The minor change is if your widget cannot return to pending state you can just initialize widget in this state and eliminate pending from stream completely. In that case you'll end up with usual Either
.
from flyd.
I've experimented with some dirty approach to this, where I pass raw Error
objects in streams. Anything except Error
is considered to be a data. It's just another way to implement Either
. The main idea is you're not obliged to wrap all your data. You just pass data as usual, but in some cases Error can occur (in that case you still need combinators to extract data or error just like with Either).
from flyd.
@StreetStrider Thank you for your feedback.
Now I understand there's cases that the approach cannot apply as you mentioned.
I think whether the approach can apply depends on what I focus, the Promise's result or state.
I like the idea to pass raw Error
objects in streams.
There must be varied ways to handle error with stream and each has slightly different meaning.
Although passing raw Error
is almost the same with Either
, it seems that it treats data
and Error
as rather close level things than Either
- Either
explicitly expresses which is "Right" while this approach does not.
I close this issue since my question is resolved.
Thank you.
from flyd.
@gitusp I've recalled related discussions #171 #35 #164 you can take a look at them.
If any traction someday will occur, I expect it to occur in that tickets.
from flyd.
I'm of the opinion that this is something that should be solved in user land.
The fact is we all have different use cases for converting promises, sometimes we need a loader, sometimes we want to maintain order, sometimes we just want all promises to push to a result stream.
In my codebase I've used:
const promiseToEitherStream = p => {
const s = stream();
p.then(val => s(Right(val))).catch(err => s(Left(err)))
return s
}
const promiseToResultStream = p => {
const s = stream(Left('pending'))
p.then(value => s(Right(value))).catch(error => s(Left(error)))
return s
}
// usage
const result = stream(url)
.map(makeRequest)
.chain(promiseToResultStream)
.map(cata({
Left: R.ifElse(R.equals('pending'), renderLoader, renderError),
Right: renderView
}))
function cata(cata) {
return monad => monad.cata(cata);
}
function renderLoader() {
return <Loader />
}
function renderError(err) {
return <Error error={err} />
}
function renderView(data) {
// ...
}
And in some cases depending on whether ordering must be preserved I use either flyd.fromPromise
or flyd.flattenPromise
.
I don't think flyd should prescribe to it's users how to interact with promises.
from flyd.
I agree with you. promiseToEitherStream
and promiseToResultStream
are both necessary depending on use cases. There should be several ways to convert a promise to a stream.
As you showed, emitting pending
as Left
seems more semantic for me, since pending
is meta information and the domain object is the promise's result.
from flyd.
Related Issues (20)
- add more dependency for existing combine HOT 7
- Merge stream delay HOT 6
- add package.module
- Drag 'n Drop example is broken HOT 2
- Compare previous state with current state HOT 14
- Feature Request - Supply metadata for a combined stream HOT 2
- Help to build control flow func HOT 3
- Wrong chain + setImmediate behavior HOT 1
- Benchmark against Most.js HOT 3
- Can flyd streams be made lazy ? HOT 9
- HELP: Trouble reading api documentation HOT 1
- Enhancement: Add Readonly-Streams (at least in Typings) HOT 3
- What's the motivation for ending a stream? HOT 3
- question on serialize stream
- from Node stream HOT 2
- Question: Required (code) complexity for higher-order streams HOT 10
- Support multiple operators for Pipe HOT 4
- [Help Needed] CombineLatest in the style of RxJs HOT 17
- Switch Latest without Ramda Dependency HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flyd.