Result
Result represents the result of an operation that can either be successful (Ok) or a failure (Error). It's commonly used to represent the result of a function that may fail, such as a network request, a file read, or a database query.
Constructors
ok
Constructs an Ok Result with the provided value.
import { Result } from 'funkcia';
// ββββ Result<number, never>
// βΌ
const result = Result.ok(10);of
Alias of Result.ok
Constructs an Ok Result with the provided value.
import { Result } from 'funkcia';
// ββββ Result<number, never>
// βΌ
const result = Result.of(10);error
Constructs an Error result with the provided value.
fromNullable
Constructs a Result from a nullable value.
If the value is null or undefined, it returns a Result.Error with a NoValueError error, or with the value returned by the provided onNullable callback. Otherwise, it returns a Result.Ok.
fromFalsy
Constructs a Result from a falsy value.
If the value is falsy, it returns a Result.Error result with a NoValueError error, or with the value returned by the provided onFalsy callback. Otherwise, it returns a Result.Ok.
try
Constructs a Result from a function that may throw.
If the function executes successfully, it returns a Result.Ok. Otherwise, it returns a Result.Error containing an UnknownError with the thrown exception, or with the value returned by the provided onThrow callback.
predicate
Returns a function that asserts that a value passes the test implemented by the provided function, creating a Result.Ok, with the value tested, if the predicate is fulfilled.
If the test fails, returns a Result.Error with a FailedPredicateError instead, or with the value returned by the provided onUnfulfilled callback.
fun
This method improves the inference of the function's return value and guarantees that it will always return a Result. It is extremely useful when your function can return multiple errors.
Declare a function that always returns a Result.
enhance
Converts a function that may throw an exception to a function that returns a Result.
values
Given an array of Results, returns an array containing only the values inside Result.Ok.
is
Asserts that an unknown value is a Result.
Instance methods
map
Applies a callback function to the value of the Result when it is Ok, returning a new Result containing the new value.
mapError
Applies a callback function to the value of the Result when it is Error, returning a new Result containing the new error value.
mapBoth
Maps both the Result value and the Result error to new values.
andThen
Applies a callback function to the value of the Result when it is Ok, and returns the new value. Similar to chain (also known as flatMap).
filter
Asserts that the Result value passes the test implemented by the provided function. Can narrow types and customize error handling.
or
Replaces the current Result with the provided fallback Result when it is Error.
swap
Swaps the Result value and error. If Ok, returns Error with the value. If Error, returns Ok with the error.
zip
Combines two Results into a single Result containing a tuple of their values, if both Results are Ok variants, otherwise, returns Result.Error.
zipWith
Combines two Results into a single Result. The new value is produced by applying the given function to both values, if both Results are Ok variants, otherwise, returns Error.
match
Compare the Result against the possible patterns and then execute code based on which pattern matches.
unwrap
Throws UnwrapError if the Result is Error.
Unwraps the Result value.
unwrapError
Throws UnwrapError if the Result is Ok.
Unwraps the Result error.
unwrapOr
Returns the value of the Result. If the Result is Error, returns the fallback value.
unwrapOrNull
Use this method at the edges of the system, when storing values in a database or serializing to JSON.
Unwraps the value of the Result if it is a Ok, otherwise returns null.
unwrapOrUndefined
Use this method at the edges of the system, when storing values in a database or serializing to JSON.
Unwraps the value of the Result if it is a Ok, otherwise returns undefined.
expect
Throws the provided Error if the Result is Error.
Unwraps the Result value.
merge
Returns the value inside the Result. If Ok, returns the value; if Error, returns the error.
contains
Returns true if the predicate is fulfilled by the wrapped value. If the predicate is not fulfilled or the Result is Error, it returns false.
toArray
Converts a Result to an array. If Result is Ok, returns an array with the value. If Result is Error, returns an empty array.
toOption
Converts a Result to an Option. If Result is Ok, returns an Option.Some. If Result is Error, returns an Option.None.
toAsyncOption
Converts the Result to an AsyncOption.
toAsyncResult
Converts the Result to an AsyncResult.
tap
Calls the function with the Result value, then returns the Result itself, ignoring the returned value of the provided function.
This allows "tapping into" a function sequence in a pipe, to perform side effects on intermediate results.
tapError
Calls the function with the Result error, then returns the Result itself, ignoring the returned value of the provided function.
This allows "tapping into" a function sequence in a pipe, to perform side effects on intermediate results.
isOk
Returns true if the Result contains a value.
isError
Returns true if the Result contains an error.
equals
Compares the Result with another Result and returns true if they are equal.
Last updated
Was this helpful?