supermacro / neverthrow

Type-Safe Errors for JS & TypeScript
MIT License
4.03k stars 84 forks source link

feat: Add unwrapOrElse #593

Closed sya-ri closed 4 weeks ago

sya-ri commented 1 month ago

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

changeset-bot[bot] commented 1 month ago

🦋 Changeset detected

Latest commit: 05f1515b9b0e1b0b535abe9f7d31c40acede9c83

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

supermacro commented 4 weeks ago

See comments in https://github.com/supermacro/neverthrow/issues/587