1.1.0 • Published 6 months ago

result4js v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Highlights

  • Type-safe error handling
  • Similar to Rust's std::Result
  • No more try-catch blocks
  • No dependencies

Install

npm i result4js
yarn add result4js
pnpm add result4js

Usage

import { Err, Ok, type Result } from "result4js";

function sum(a: any, b: any): Result<number, string> {
    if (typeof a !== "number" || typeof b !== "number") return Err("Both arguments are not numbers");
    return Ok(a + b);
}

const goodResult = sum(1, 2); // Success(3)
const badResult = sum("1", 2); // Failure("Both arguments are not numbers")

// Handle results with type checking
if (goodResult.isErr()) {
  // Handle error case
  return;
}
const goodData = goodResult.data; // 3

// Or use helper methods

// Throws the error if the result is Failure
// Only use this when you're absolutely certain the operation will succeed
const badData = badResult.throw();

// Provide a fallback value
// This is always safe
const badData = badResult.or(0); // 0
1.1.0

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago