Comments (6)
Based on what you've given me here's how the implementation must look like:
interface User { sellerId: number }
interface SellerDetails { id: number }
declare const getUser: () => Either<Error, User>
declare const getUserSeller: (sellerId: number) => Promise<Either<Error, SellerDetails>>
const execute = (): Promise<Error | SellerDetails> =>
EitherAsync.liftEither(getUser())
.map((user) => user.sellerId)
.chain((sellerId) => getUserSeller(sellerId))
.run()
.then((either) => either.extract())
Hope that helps! The key here is that if you have an Either and you want to do async stuff with it you can use EitherAsync.liftEither
and then you're free to chain Promises. Also if you decide to return EitherAsync directly it's less code to wrap and unwrap values:
const execute = (): EitherAsync<Error, SellerDetails> =>
EitherAsync.liftEither(getUser())
.map((user) => user.sellerId)
.chain((sellerId) => getUserSeller(sellerId))
const result: Either<Error, SellerDetails> = await execute()
from purify.
Without more context I can't help you much, what does this.stateManager?.getUser(state)
return? An Either, Maybe, EitherAsync?
I would prefer a codepen/stackblitz etc with code I can work with.
from purify.
Either
only works synchronously, even if you use async/await inside map
it's still a Promise and it's much like using async/await inside Array.map
- it doesn't work.
I suggest you look at the EitherAsync docs, the API is exactly the same as Either but you can work with Promises and async/await.
from purify.
The most funny thing is it code works :)) and Idea's linter is OK with it. But not with chain
Is it right?
async execute(state: StateDto): Promise<NetworkError | SellerDetailsDto> {
return await this.stateManager?.getUser(state)
.map((user) => user.seller?.id)
.map(async (sellerId) => {
return EitherAsync(() => this.userService.getUserSeller(sellerId ?? ''));
})
.extract();
}
So it works too. :-))
from purify.
Unfortunately I don't know how to use it. There was no need earlier. :((
this.stateManager?.getUser(state)
returns Either<any, UserDto>
this.userService.getUserSeller(sellerId ?? '')
returns Promise<Either<NetworkError, SellerDetailsDto>>
from purify.
It is a HUUUGE HELP! Thank you very much!!!
Real world examples are extremely needed!
That's the final working result:
async execute(state: StateDto): Promise<NetworkError | SellerDetailsDto> {
return EitherAsync.liftEither(this.stateManager.getUser(state))
.map((user) => user.seller?.id)
.chain((sellerId) => this.userService.getUserSeller(sellerId ?? ''))
.run()
.then((either) => either.extract());
}
}
BTW
If my first WORKING use case is not expectable it is ponentialy a bug or a promising feature! ;-)
from purify.
Related Issues (20)
- Add ES modules in the npm package HOT 4
- [feature request] Add IO monad HOT 1
- Just Date gives unexpected result when applying JSON.stringify() HOT 1
- Crashes because of use of `JSON.stringify` in Codec module HOT 3
- Incorrect typing of EitherAsync.map HOT 2
- 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
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.