Code Monkey home page Code Monkey logo

Comments (17)

mustafaekim avatar mustafaekim commented on August 27, 2024

I realise that this code is completely broken because even if the either inside map returns a left, I cannot catch it in mapLeft. So I certainly need a replacement with chain

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

Any idea on how I can pass an async function into a chain? I cannot transform an Either to another Either asynchronously.

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

Promise and Either are both monads, and monads do not compose. Unfortunately, there's no easy way to do what you're trying to do in the context of this library, but I see that there is a problem and I'll try to address that in the next version of purify.

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

Oh ok, I will write a utility function to do that. Thanks

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

Here's an example API that I'm thinking about (this is just a draft):

async function run() {
  const either = await getFormInstance()
  const data = await either.chainAsync(doc => getTokenObjectValidForFormInstance(doc.data))

  return data
    .map(tokenObject => OK(tokenObject))
    .mapLeft(err => ERROR(err)).extract()
}

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

Yes exactly but I was planning to build --just as an external utility function

chainAsync will be perfect.

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

Hi, is there any guess on when you will implement, I would prefer to use the library method

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

I have no estimations, sorry. The good news is that I'm currently dogfooding this library in my own backend, so I experience all of the issues you mentioned and I'll have a better idea on how to fix them.

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

OK, I see that the code is updated with async map/chain in the repository. However I was thinking that map already works with async function, doesn't it?

I hope that the npm package will be updated, thanks

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

The problem with async is that it gets nested inside the Either, for example:

either.map(x => getUserById(x))

will result in the type Either<something, Promise<User>>, which is hard to work with, you have to combine both Promise.then and either functions to work with the user.
If you use mapAsync though, the type becomes Promise<Either<something, User>>, you can await that to get just an either of user.

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

After giving it some thought I really don't like the way mapAsync and chainAsync work currently. I'll be researching other ways to accomplish an elegant async API.

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

OK. I have to drop the library because I cannot advance in the current situation.. That will cause a lot of work to be redone but it seems like there is no easy solution for now. Without chain, the only solution left is to use unsafeCoerce and catch the error, which makes the code very ugly.

It would be OK to have chain accepting an async function even though it returns a promise of either. I am using map in the same manner.

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

below is how I try to handle multiple async calls

  try {
    var organizationDoc = (await service.organization.get(body.organizationId)).unsafeCoerce()
    if (!organizationDoc.data.settings.signUp.allow) return NOK(res, ServerError.somethingIsWrong)

    var either = await service.organization.addUser(body.organizationId, body.fullname, body.username, body.password, organizationDoc.data.settings.signUp.tags, body.email);
    return either
      .map(async (doc: Doc<User>) => {
        var userCard: Card = await service.authentication.createUserCard(body.organizationId, doc);
        return OK(res, bZetOK<Card>(userCard));
      })
      .mapLeft((err: ZetError) => NOK(res, err))
      .extract()
  }
  catch (err) {
    return ERROR(res, err);
  }

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

Sorry for causing you to rewrite your code, as you can see the library is still in early development and there are a lot of important decisions ahead and I don't want to rush these things.

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

I spend some time analyzing your code and I see a couple of problems. You really shouldn't try to mix imperative if statements, async/await and early returns, it's not going to work. I did some refactoring and here's how I would write this code:

const isSignUpAllowed = (doc: Doc): Either<ServerError, Doc> =>
  doc.allow ? Right(doc) : Left(ServerError.somethhingIsWrong)

serviceOrganizationGet().then(organizationDoc =>
  organizationDoc
    .chain(x => isSignUpAllowed(x))
    .map(doc => addUser(doc.tags))
    .map(userPromise =>
      userPromise
        .then(user => createUserCard(user))
        .then(userCard => bZetOK(userCard))
    )
    .mapLeft(err => NOK(err))
    .extract()
)

Please let me know if that helps!

from purify.

mustafaekim avatar mustafaekim commented on August 27, 2024

What if map(userPromise) fails? I end up with a Left inside a Right situation, no? If so, it will bypass the mapLeft that follows.

from purify.

gigobyte avatar gigobyte commented on August 27, 2024

Good question! I guess I didn't test this enough. I'll close this topic as we're getting off topic now, as I said I'll give it some thought and I'll avoid posting more not working code 😅

from purify.

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.