Coder Social home page Coder Social logo

result_rs's Introduction

result-rs

Rust's Result enum ported to Typescript/Javascript

Installation

Import the module to your code:

import { Result } from 'https://deno.land/x/result_rs/mod.ts';

It is also published as a npm module as result_rs.

$ npm i result_rs
# or using pnpm
$ pnpm add result_rs
# or yarn
$ yarn add result_rs

Then you can import it in your code:

// Commonjs require
const { Result } = require('result_rs');
// ES Module import
import { Result } from 'result_rs';

Usage

Use the Result.from method to create a new instance of Result

const result = Result.from(someFunctionThatThrows);

result.is_ok(); // Returns false if function throwed an error

The package also exports two utility functions Ok and Err that are useful to create Ok or Err values from simple values instead of a function

import { Err, Ok } from 'https://deno.land/x/result_rs/mod.ts';

const ok = Ok('Hello World');

ok.is_ok(); // true;
ok.unwrap(); // 'Hello World'

const err = Err('Error value');
err.is_er(); // true;
err.unwrap(); // throws an error

Passing async functions to the Result.from method are handled like synchronous functions. If you want to resolve the internal value's promise, you can use the Result.sync method on any instance to resolve the promise internally.

const result = Result.from(async () => 'Hello World');
result.unwrap(); // Promise<string>

await result.sync();
result.unwrap(); // 'Hello World'

Documentation of all methods is available here.

Author

result-rs © Yakiyo. Authored and maintained by Yakiyo.

Released under MIT License

result_rs's People

Contributors

yakiyo avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.