Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aleksrutins/result
https://github.com/aleksrutins/result
Last synced: about 22 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/aleksrutins/result
- Owner: aleksrutins
- Created: 2024-06-20T00:43:10.000Z (5 months ago)
- Default Branch: master
- Last Pushed: 2024-06-20T00:55:25.000Z (5 months ago)
- Last Synced: 2024-06-20T13:16:29.359Z (5 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
[![JSR](https://jsr.io/badges/@asr/result)](https://jsr.io/@asr/result)
[![JSR Score](https://jsr.io/badges/@asr/result/score)](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'
```