Comments (17)
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.
Any idea on how I can pass an async function into a chain? I cannot transform an Either to another Either asynchronously.
from purify.
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.
Oh ok, I will write a utility function to do that. Thanks
from purify.
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.
Yes exactly but I was planning to build --just as an external utility function
chainAsync will be perfect.
from purify.
Hi, is there any guess on when you will implement, I would prefer to use the library method
from purify.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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)
- Different Behavior Between EitherAsync .chain(x => x) and .join() HOT 4
- Guidance on Extending Classes HOT 6
- esm version missing index.js HOT 12
- Make Pattern Follow TypeScript Naming Scheme HOT 4
- Use property based testing HOT 2
- EQ Combinator HOT 2
- apChain? HOT 4
- please clarify deno support HOT 2
- Discord HOT 1
- widen output types for `Maybe.mapOrDefault` HOT 1
- Adding a new type for representing the "state" of an async operation HOT 2
- EitherAsync's bimap does not behave as the mapLeft for PromiseLike Left values HOT 3
- Release 2.0.2 breaks esm build for packages with "type": "module" in their package.json HOT 4
- EitherAsync handling uncaught exceptions HOT 1
- Badges Broken HOT 4
- Typescript 5.5 types behave poorly HOT 7
- Codec with optional `unkown` has unexpected type `unknown` instead of `unknown | undefined` HOT 2
- Additional types HOT 2
- add some List functions? HOT 1
- Support a "left" constructor, for example in EitherAsync HOT 1
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 purify.