-
Notifications
You must be signed in to change notification settings - Fork 84
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 request] have .unwrapOr()
equivalent that runs a function
#587
Comments
Hello @janglad I believe const user = await getSignedInUser().match( (user) => user, (e) => { redirectToAuth() }); |
@janglad |
@mattpocock not quite, still leaves you with a Result interface User {
id: string;
}
interface AuthError {
code: string;
}
declare const getUser: () => ResultAsync<User, AuthError>;
declare const redirect: () => never;
const user = await getUser().mapErr(redirect);
// Result<User,never>
const user2 = await getUser().match((user) => user, redirect);
// User while not super on topic for this issue this does make me wonder if a @paduc that's true, but while obv a quick |
@janglad I believe what you are trying to do may be outside the scope of this lib, which is handling error types. You are trying to add behavior / actions. There are plenty of ways to achieve this without adding to the API. |
@paduc yea agree that redirecting is def out of scope, but not sure if using a function to get the fallback is? Similar to how on Java optionals you have |
I'm not sure if emulating Rust's |
Correct. https://gdelgado.ca/neverthrow-subtyping#title Also why does |
result.match((x) => x, () => 'an error')
result.unwrapOr('an error')
// similarly
result.match((x) => x, (error) => JSON.stringify(error))
result.unwrapOrElse((error) => JSON.stringify(error)) |
@iyefrat I agree that |
oh sure, I just didn't really have a default value to use as a generic example, but you're right that a better sketch would be: result.match((x) => x, () => 'default')
result.unwrapOr('default')
// similarly
result.match((x) => x, (error) => recoverFromError(error))
result.unwrapOrElse((error) => recoverFromError(error)) |
Title, probably best to implement it as a new method as this could be breaking for people that are currently returning functions from
.unwrapOr()
.Example usecase specific usecase for me
but a more common use case would be having a dynamic value as a fallback (
result.error
could be provided as argument) or one that you want to compute lazily.If this is something other people want please leave a suggestion for naming and I can probably implement this.
The text was updated successfully, but these errors were encountered: