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

Async/Promise support – gathering information #79

Closed
jstasiak opened this issue Sep 4, 2023 · 3 comments
Closed

Async/Promise support – gathering information #79

jstasiak opened this issue Sep 4, 2023 · 3 comments

Comments

@jstasiak
Copy link
Collaborator

jstasiak commented Sep 4, 2023

Relevant issue in the upstream repository: vultix#33

Basically it's really inconvenient to mix Option and Result with promises right now because a Promise<Option<...>> or Promise<Result<...>> needs to be awaited first in order to use any of the composition/combination methods (like map(), andThen() etc.)

Some code I wrote today:

// readFileChecked() returns Promise<Result<Buffer, ...>>
// parseCsvChecked() takes Buffer and returns Result<object[], ...>

const rawContent = await readFileChecked(path)
if (rawContent.isErr()) {
    return rawContent
}
return rawContent.andThen(parseCsvChecked)

What I wish I could write is:

return readFileChecked(path).andThen(parseCsvChecked)

Any ideas about the API or the design of this feature are welcome.

@jstasiak
Copy link
Collaborator Author

jstasiak commented Sep 5, 2023

Some ideas off the top of my head:

(In this context an async function is something that returns Promise<Result<>> or AsyncResult<>).

  • We need something like AsyncOption and AsyncResult and they need to support most of the existing Option and Result API
  • Given a Result I should be able to compose it with an async function and get an AsyncResult, for example:
syncResult: Result<number, Error>
asyncFunction: (number) -> Promise<Result<string, Error>>

syncResult.andThen(asyncFunction) // -> AsyncResult<string, Error>
  • Given an AsyncResult I should be able to compose with with sync function and async function and in both cases get AsyncResult, for example:
asyncResult: AsyncResult<number, Error>
asyncFunction: (number) -> Promise<Result<string, Error>>
syncFunction: (string) -> Result<number[], Error>>

asyncResult.andThen(asyncFunction).andThen(syncFunction) // -> AsyncResult<number[], Error>
  • Awaiting AsyncResult<T, E> should yield Result<T, E> I think?

@jstasiak
Copy link
Collaborator Author

#87 adds some initial async support. Once it's merged we'll use it for a bit in the Lune backend to see if it's satisfactory.

@jstasiak
Copy link
Collaborator Author

We've implemented some more stuff since then:

I'll close this ticket, the feature exists, we can iterate on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant