Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/result_js/mod.ts

Minimum result type port of Rust
Latest
import * as resultJs from "https://deno.land/x/result_js@2.0.0/mod.ts";

Error handling with the Result type. Result<T, | E> is the type used for returning and propagating errors. It is an enum with the variants, Ok<T>, representing success and containing a value, and Err<E>, representing error and containing an error value.

Operators overview

Result provides a wide variety of different operators.

Querying the variant

The isOk and isErr return true if the Result is Ok or Err, respectively.

Extracting contained values

Extract the contained value in a Result<T, | E> when it is the Ok. If the Result is Err:

  • expect throws with a provided custom message
  • unwrap throws with a generic message
  • unwrapOr returns the provided default value
  • unwrapOrElse returns the result of evaluating the provided function

Extract the contained value in a Result<T, | E> when it is the Err. If the Result is Ok:

Transforming contained values

Transforms the contained value of the Ok:

  • map transforms Result<T, | E> into Result<U, | E> by applying the provided function to the contained value of Ok and leaving Err

Transforms the contained value of the Err:

  • mapErr transforms Result<T, | E> into Result<T, | F> by applying the provided function to the contained value of Err and leaving Ok

Transform a Result<T, | E> into a value of a possibly different type U:

  • mapOr applies the provided function to the contained value of Ok, or returns the provided default value if the Result is Err
  • mapOrElse applies the provided function to the contained value of Ok, or applies the provided default fallback function to the contained value of Err

Logical operators

Treat the Result as a boolean value, where Ok acts like true and Err acts like false.

The and and or take another Result as input, and produce a Result as output. The and can produce a Result<U, | E> value having a different inner type U than Result<T, | E>. The or can produce a Result<T, | F> value having a different error type F than Result<T, | E>.

name result input output
and ok result result
and err result err
or ok result ok
or err result result

The andThen and orElse take a function as input, and only evaluate the function when they need to produce a new value. The andThen can produce a Result<U, | E> value having a different inner type U than Result<T, | E>. The orElse can produce a Result<T, | F> value having a different error type F than Result<T, | E>.

name result function input function result output
andThen Ok<T> T result result
andThen err - - err
orElse ok - - ok
orElse Err<E> E result result

Enums

The result type.

Variables

Err constructor.

Ok constructor.

Functions

Returns res if the result is Ok, otherwise returns the Err.

Calls fn if the result is Ok, otherwise returns the Err.

Returns the contained Ok value.

Returns the contained Err value.

Returns true if the result is a Err.

Returns true if the result is a Ok.

Maps a Result<T, | E> to Result<U, | E> by applying fn to a contained Ok, leaving an Err.

Maps a Result<T, | E> to Result<T, | F> by applying fn to a contained Err value, leaving an Ok.

Returns the provided defaultValue (if Err), or applies fn to the contained value (if Ok),

Maps a Result<T, | E> to U by applying defaultFn to a contained Err, or fn to a contained Ok.

Pattern matching for result. Match on matcher.Ok if Ok, otherwise match on matcher.Err.

Returns res if the result is Err, otherwise returns the Ok.

Calls fn if the result is Err, otherwise returns the Ok.

Returns the contained Ok value.

Returns the contained Err value.

Returns the contained Ok value, otherwise defaultValue.

Returns the contained Ok value, otherwise computes it from a closure.

Interfaces

The Err API.

Err constructor.

Result matcher.

The Ok API.

Ok constructor.

Type Aliases

Representation of Ok or Err.