https://github.com/aleksrutins/result
https://github.com/aleksrutins/result
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/aleksrutins/result
- Owner: aleksrutins
- Created: 2024-06-20T00:43:10.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-06-20T00:55:25.000Z (about 1 year ago)
- Last Synced: 2025-03-10T19:17:59.589Z (4 months ago)
- Language: TypeScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# @asr/result
[](https://jsr.io/@asr/result)
[](https://jsr.io/@asr/result)A simple library for result-based error handling, inspired by Rust's `Result`.
A code block is worth a thousand words:
```ts
import { Result } from '@asr/result';const okResult = Result.ok('hello, world');
const errorResult = Result.error('not OK');okResult.unwrap() // 'hello, world'
errorResult.unwrap() // ResultError: not OKerrorResult.unwrapOr('rescued from certain doom') // 'rescued from certain doom'
errorResult.unwrapOrElse(() => 'another value') // 'another value'
okResult.ok() // true
errorResult.ok() // falseokResult.map(it => it + ", Joe").unwrap() // 'hello, world, Joe'
errorResult.mapError(it => it + ", Houston").error // 'not OK, Houston'
```