Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aleksrutins/result


https://github.com/aleksrutins/result

Last synced: about 22 hours ago
JSON representation

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 OK

errorResult.unwrapOr('rescued from certain doom') // 'rescued from certain doom'

errorResult.unwrapOrElse(() => 'another value') // 'another value'

okResult.ok() // true
errorResult.ok() // false

okResult.map(it => it + ", Joe").unwrap() // 'hello, world, Joe'
errorResult.mapError(it => it + ", Houston").error // 'not OK, Houston'
```