Skip to content

Can EitherAsync.chain support both Either and EitherAsync? #731

Open
@lightDer

Description

Hi! It's really a neat and lightweight libs.
I wonder if EitherAsync.chain can support both Either and EitherAsync?

For example, if I want to chain getTitle, I need add async in the chain method.
Or any recommendations for handling this? Thanks.

async function getPost (postId: number): Promise<Either<Error, Post>> {
  const response = await fetch(`/posts/${postId}`);
  return response
    ? Right((response.json() as unknown) as Post)
    : Left(new Error(`Problem fetching post with id: ${postId}`));
}

function getTitle (post: Post): Either<Error, string> {
  return post.title
    ? Right(post.title)
    : Left(new Error("This post should have a title"));
}

function getPostTitle (req: Request): EitherAsync<Error, string> {
  return EitherAsync.liftEither(validateRequest(req))
    .chain((request) => getPost(request.postId))
    .chain(async (post) => getTitle(post));
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions