sweet-monads icon indicating copy to clipboard operation
sweet-monads copied to clipboard

I'd like to see EitherAsync, MaybeAsync and etc

Open wennerryle opened this issue 1 year ago • 4 comments

import { left, right, type EitherAsync } from "@sweet-monads/either";
import { just, none, type Maybe } from "@sweet-monads/maybe";
import type { Order } from "../types/order";
import type { RetailCRM_Config } from "../request";

import { NetworkError } from "$lib/core/errors/NetworkError";

export async function getOrder(
  config: RetailCRM_Config,
  id: number
): EitherAsync<NetworkError, Maybe<Order>> {

instead of:

import { left, right, type EitherAsync } from "@sweet-monads/either";
import { just, none, type Maybe } from "@sweet-monads/maybe";
import type { Order } from "../types/order";
import type { RetailCRM_Config } from "../request";

import { NetworkError } from "$lib/core/errors/NetworkError";

export async function getOrder(
  config: RetailCRM_Config,
  id: number
): Promise<Either<NetworkError, Maybe<Order>>> {

importance: make my life easier

At the moment I do it this way: app.d.ts:

declare module "@sweet-monads/either" {
  type EitherAsync<L, R> = Promise<import("@sweet-monads/either").Either<L, R>>;
}

I love your library. Thank you!

wennerryle avatar Apr 09 '25 12:04 wennerryle

Hi @wennerryle. Thank you so much for your feedback; I'm thrilled to hear it ❤️ May I ask you just a little bit to clarify the issue? Would you like to see such aliases for each type or some additional API? What kind of API is missing if the answer is also an API?

JSMonk avatar Apr 13 '25 03:04 JSMonk

Thank you for response :) Yep, you're correct, I'd like to see such aliases for each type

wennerryle avatar Apr 13 '25 09:04 wennerryle

@JSMonk I like your library, however chaining promises handling with Either.chain still looks not very convenient. In purify-ts or purifree-ts they have a class EitherAsync which allows to handle promises in pretty elegant way

const result = EitherAsync.fromPromise(api.getUsers)
  .map(response => response.users)
  .map(sortAndFilterUsers)
  .run()

Would you consider this useful to implement?

AndrewGapon avatar Jun 19 '25 08:06 AndrewGapon

@AndrewGapon, from the API perspective, it looks good. However, I'm not sure there will be no de-optimizations from the VM sides when using thenable objects instead of the regular Promise. https://github.com/v8/promise-performance-tests

JSMonk avatar Jun 29 '25 16:06 JSMonk