JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 15
  • Score
    100M100P100Q80771F
  • License MIT

resulta is a TypeScript utility for handling results in an expressive way, inspired by Rust's Result type.

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resulta) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Resulta

    Resulta is a TypeScript library that provides a Result type for handling success and error values in a functional way. It is inspired by the Result type in Rust.

    Installation

    You can install Resulta using npm:

    npm install resulta

    Or using yarn:

    yarn add resulta

    Usage

    Basic Usage

    import { ok, err, Result } from 'resulta';
    
    function hello(message = ''): Result<string, Error> {
        if (!message) {
            return err(new Error('hello without world'));
        }
    
        return ok(`hello ${message}`);
    }
    
    const result = hello();
    
    if (result.ok) {
        console.log(result.value);
    } else {
        console.error(result.error);
    }

    Async Functions

    import { tryCatchAsync } from 'resulta';
    
    async function fetchData(): Promise<string> {
        // Simulate an async operation
        return "data";
    }
    
    async function main() {
        const result = await tryCatchAsync(fetchData);
    
        if (result.ok) {
            console.log(result.value);
        } else {
            console.error(result.error);
        }
    }
    
    main();

    API

    ok<T>(value: T): Ok<T>

    Returns an object representing an Ok result.

    err<E>(error: E): Err<E>

    Returns an object representing an Err result.

    tryCatchAsync<T, E>(fn: () => Promise<T>): Promise<Result<T, E>>

    Executes a provided asynchronous function and returns a Result type.

    License

    This project is licensed under the MIT License - see the LICENSE file for details.