Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Either async support #50

Closed
2 tasks done
Wolfremium13 opened this issue Nov 1, 2024 · 0 comments · Fixed by #56
Closed
2 tasks done

[FEATURE] Either async support #50

Wolfremium13 opened this issue Nov 1, 2024 · 0 comments · Fixed by #56
Assignees
Labels
enhancement New feature or request

Comments

@Wolfremium13
Copy link
Contributor

Why do we need this improvement?

To generate interoperability between the synchronous and asynchronous monads in the library.

How will this change help?

This change will help concatenate synchronous and asynchronous calls by transforming an Either into a Future. This way, we won’t need to manually map the call; instead, by invoking the method, we make it asynchronous, allowing us to use async maps.

Screenshots

No response

How could it be implemented/designed?

Here's an example case:

  it.each([
    {
      type: 'Right',
      either: Either.right(2),
      syncClosure: (x: number) => x * 2,
      asyncClosure: async (x: number) => x * 2,
      expected: 8,
    },
  ])(
    '$type should handle map with another async map operation correctly',
    async ({ either, syncClosure, asyncClosure, expected }) => {
      either
        .map(syncClosure)
        .toFuture()
        .map(asyncClosure)
        .complete(
          async (value) => expect(await value).toEqual(expected),
          async (error) => expect(error).toBeUndefined()
        );
    }
  );

Where in abstract class we add:

// abstract class Either<L, R>
abstract toFuture(): Future<R | L>;

And in its implementations:

// class Left<L, R>
toFuture(): Future<L> {
    return Future.of(() => Promise.resolve(this.value));
}
// class Right<L, R>
toFuture(): Future<R> {
    return Future.of(() => Promise.resolve(this.value));
}

🚧 Breaking changes

No

👀 Have you checked for similar open issues?

  • I checked and didn't find a similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue?

Yes I am willing to submit a PR!

@Wolfremium13 Wolfremium13 added the enhancement New feature or request label Nov 1, 2024
@Wolfremium13 Wolfremium13 self-assigned this Nov 1, 2024
@Marius9595 Marius9595 linked a pull request Nov 1, 2024 that will close this issue
8 tasks
@Marius9595 Marius9595 linked a pull request Nov 11, 2024 that will close this issue
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
1 participant