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

feat: Add unwrapOrElse #593

Closed
wants to merge 3 commits into from
Closed

Conversation

sya-ri
Copy link

@sya-ri sya-ri commented Oct 10, 2024

Similar to unwrap, but returns default value by function: #587

const myResult = err('Oh noooo')

const multiply = (value: number): number => value * 2

const unwrapped: number = myResult.map(multiply).unwrapOrElse(() => 10)
const unwrapped: number = await errAsync(0).unwrapOrElse(() => 10)
// unwrapped = 10

Useful for lazily evaluating defaults or performing processing.

unwrapOrElse exists in Rust:
https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or_else

Copy link

changeset-bot bot commented Oct 10, 2024

🦋 Changeset detected

Latest commit: 05f1515

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
neverthrow Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

unwrapOrElse<A>(err: (e: E) => A): Promise<T | A> {
return this._promise.then((res) => {
if (res.isErr()) {
return err(res.error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reading this was a little confusing because of the err function in the library :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Rereading, I was confused too 😆

I made it better 😉

0f34663

@sya-ri sya-ri requested a review from macksal October 15, 2024 03:15
Comment on lines +462 to +464
unwrapOrElse<A>(err: (e: E) => A): T | A {
return err(this.error)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still find the naming err to be a bit confusing, but now I see that it's used everywhere for this type of argument 🤷🏼 e.g. in match directly below. no reason to break that pattern...

Comment on lines +207 to +210
it('unwrapOrElse and return the Ok value', () => {
const okVal = ok(12)
expect(okVal.unwrapOrElse(() => 1)).toEqual(12)
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's part of unwrapOrElse's contract to not call the callback for an Ok value.

It could make sense to test that (using jest.fn and expect(...).not.toHaveBeenCalled())

@supermacro
Copy link
Owner

supermacro commented Oct 24, 2024

See comments in #587

@supermacro supermacro closed this Oct 24, 2024
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

Successfully merging this pull request may close these issues.

3 participants