-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Wrap async/sync result in the library #111
Comments
I think your proposal is interesting. Have you considered using function resultOf<T, E = unknown>(val: () => T): Result<T, E> {
try {
return Ok(val());
} catch(err) {
return Err(err);
}
}
// usage:
const result = resultOf(() => 10 / 0); Also, I have modified the error type, as we cannot be certain that the thrown value is an error. upd: I hadn't seen your PR #112 , when I sent the comment. |
My bad, I should have linked this issue with my PR.
And from non-async functions i meant regular functions, yes. |
Would be cool to see this implemented :) |
Its super common for async/sync operations to throw an error, but i don't see a constructor to conveniently wrap the operation in the
Result
. I propose a new constructor:This allows us to go from the throw/catch error handling to the monadic error handling patterns
const result = await AsyncResultOf(fetch(...))
I will go further to suggest an even more general constructor that translates both async and non-async fallible operations:
This can of course be overloaded with the signature of
AsyncResultOf
.(PS im trying to whip up a PR but the test keeps saying "Timed out while running tests" on my machine)
The text was updated successfully, but these errors were encountered: