Skip to main content
Home

Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score94%
Publisheda year ago (0.0.4)

INTERNAL

class Result

Result allows you to show to a consumer that a function might throw and force them to handle it.

T Value type

E Error type

Usage

function functionThatMightFail(): Result<T, E>;

Examples

const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");

const result = functionThatMightFail();

console.log(result.unwrap()); // "Hello, World!"

Constructors

new
Result(result: _Result<T, E>)

Type Parameters

Properties

private
readonly
_result: _Result<T, E>

Methods

expect(message: string): T

Tries to return value if value is Err throws custom error message.

expectErr(message: string): E

Tries to return error value if value is Ok throws custom error message

Returns true if result is Err

Returns true if result is Ok

map<A>(fn: (val: T) => A): Result<A, E>

Maps Result<T, E> to Result<A, E> using the passed mapping function

mapErr<A>(fn: (err: E) => A): Result<T, A>

Maps Result<T, E> to Result<T, A> using the passed mapping function

mapErrOr<A>(
defaultVal: A,
fn: (err: E) => A,
): A

In the Err case returns the mapped value using the function else returns defaultVal

mapErrOrElse<A>(
def: (val: T) => A,
fn: (err: E) => A,
): A

In the Err case returns the mapped value using the function else returns value of def

mapOr<A>(
defaultVal: A,
fn: (val: T) => A,
): A

In the Ok case returns the mapped value using the function else returns defaultVal

mapOrElse<A>(
def: (err: E) => A,
fn: (val: T) => A,
): A

In the Ok case returns the mapped value using fn else returns value of def

match<
A,
B = A,
>
(
success: (val: T) => A,
failure: (err: E) => B,
): A | B

Allows you to run callbacks based on the result.

Tries to return value if value is Err throws generic error message.

Tries to return err if value is Ok throws generic error message.

unwrapErrOr(defaultVal: E): E

Tries to unwrap the error if vale is Ok returns defaultVal

unwrapErrOrElse(fn: (val: T) => E): E

Tries to return the error if value is Ok calls fn

unwrapOr(defaultVal: T): T

Tries to unwrap the value if value is Err returns defaultVal

unwrapOrElse(fn: (err: E) => T): T

Tries to return the value if value is Err calls fn

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@logix/internal-utils

Import symbol

import { Result } from "@logix/internal-utils";
or

Import directly with a jsr specifier

import { Result } from "jsr:@logix/internal-utils";

Add Package

pnpm i jsr:@logix/internal-utils
or (using pnpm 10.8 or older)
pnpm dlx jsr add @logix/internal-utils

Import symbol

import { Result } from "@logix/internal-utils";

Add Package

yarn add jsr:@logix/internal-utils
or (using Yarn 4.8 or older)
yarn dlx jsr add @logix/internal-utils

Import symbol

import { Result } from "@logix/internal-utils";

Add Package

vlt install jsr:@logix/internal-utils

Import symbol

import { Result } from "@logix/internal-utils";

Add Package

npx jsr add @logix/internal-utils

Import symbol

import { Result } from "@logix/internal-utils";

Add Package

bunx jsr add @logix/internal-utils

Import symbol

import { Result } from "@logix/internal-utils";