GithubHelp home page GithubHelp logo

Comments (6)

StreetStrider avatar StreetStrider commented on July 30, 2024

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.

StreetStrider avatar StreetStrider commented on July 30, 2024

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.

gitusp avatar gitusp commented on July 30, 2024

@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.

StreetStrider avatar StreetStrider commented on July 30, 2024

@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.

nordfjord avatar nordfjord commented on July 30, 2024

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.

gitusp avatar gitusp commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.