The type of the value contained in a successful Result.
The type of the error contained in an unsuccessful Result.
Readonly[A unique symbol property used to identify the variant of this Result.
Returns 'Ok' if the Result represents success, or 'Err' if it represents failure.
This is used internally by the isResult utility function to verify that an object is a valid Result instance,
and to distinguish between Ok and Err variants without calling methods.
Note: The symbol itself is not exported as part of the public API.
Use the isResult utility function or the isOk()/isErr() methods for type checking.
Readonly[toThe well-known symbol Symbol.toStringTag used by Object.prototype.toString().
Returns 'Result' so that Object.prototype.toString.call(result) produces '[object Result]'.
This enables reliable type identification even across different execution contexts (e.g., iframes, different module instances).
Implements the Iterator protocol, allowing Result to be used with for...of loops and spread syntax.
Ok(value), yields the contained value once.Err(error), yields nothing (empty iterator).This is similar to Rust's Result::iter() method.
An iterator that yields zero or one value.
// Using with for...of
for (const value of Ok(5)) {
console.log(value); // 5
}
for (const value of Err('error')) {
console.log(value); // never executed
}
// Using with spread operator
const arr1 = [...Ok(5)]; // [5]
const arr2 = [...Err('error')]; // []
// Using with Array.from
Array.from(Ok('hello')); // ['hello']
Array.from(Err('error')); // []
Asynchronous version of andThen.
The type of the value returned by the function.
A promise that resolves to this as Err if this is Err, otherwise the result of fn.
Non-standard extension: This method is not part of Rust's standard library.
Like andThenAsync, but automatically catches any thrown exceptions or Promise rejections
and converts them to Err.
This is useful when calling async functions that may throw, without needing to wrap
the call in tryAsyncResult.
Note: The error type E is preserved. If the function throws, the thrown value
is cast to E. The caller is responsible for ensuring that thrown exceptions
are compatible with type E, similar to how tryAsyncResult<T, E> works.
The type of the value returned by the function.
A promise that resolves to Ok(U) if successful, or Err(E) if this is Err or if fn throws/rejects.
const x: Result<number, Error> = Ok(2);
// If the async function succeeds
const result = await x.andTryAsync(async v => {
const response = await fetch(`/api/multiply/${v}`);
return response.json();
});
console.log(result.unwrap()); // { value: 4 }
// If the async function throws or rejects
const failed = await x.andTryAsync(async v => {
throw new Error('Network error');
});
console.log(failed.unwrapErr()); // Error: Network error
// If this is already Err, fn is not called
const y: Result<number, Error> = Err(new Error('original error'));
const skipped = await y.andTryAsync(async v => v * 2);
console.log(skipped.unwrapErr()); // Error: original error
Transforms the current Result into a new Result where the type of the success value is replaced with a new type U.
The type of the error remains unchanged.
This is a type-level only operation, equivalent to result as unknown as Result<U, E>.
Useful when you need to propagate an error to a different success type context.
The new type for the success value.
this with the success type cast to U.
Transforms the current Result into a new Result where the type of the error is replaced with a new type F.
The type of the success value remains unchanged.
This is a type-level only operation, equivalent to result as unknown as Result<T, F>.
The new type for the error.
this with the error type cast to F.
Tests whether this and other are both Ok containing equal values, or both are Err containing equal errors.
true if this and other are both Ok with equal values, or both are Err with equal errors, otherwise false.
Returns the contained Ok value, with a provided error message if the result is Err.
The error message to provide if the result is an Err.
Returns the contained Err value, with a provided error message if the result is Ok.
The error message to provide if the result is an Ok.
Calls the provided function with the contained value if this is Ok, for side effects only.
Does not modify the Result.
A function to call with the Ok value.
this, unmodified.
Calls the provided function with the contained error if this is Err, for side effects only.
Does not modify the Result.
A function to call with the Err value.
this, unmodified.
Returns the contained Err value.
Unlike unwrapErr, this method is known to never throw because
the success type is never, meaning the Ok variant can never occur.
This provides a compile-time guarantee that the extraction is safe. If someone later changes the ok type to one that can occur, the code will fail to compile rather than potentially throwing at runtime.
The contained Err value.
Returns the contained Ok value.
Unlike unwrap, this method is known to never throw because
the error type is never, meaning the Err variant can never occur.
This provides a compile-time guarantee that the extraction is safe. If someone later changes the error type to one that can occur, the code will fail to compile rather than potentially throwing at runtime.
The contained Ok value.
Returns true if the result is Err and the provided predicate returns true for the contained error.
A function that takes the Err value and returns a boolean.
Asynchronous version of isErrAnd.
A function that takes the Err value and returns a PromiseLike<boolean> or boolean.
A promise that resolves to true if the Result is Err and the predicate resolves to true.
Returns true if the result is Ok and the provided predicate returns true for the contained value.
A function that takes the Ok value and returns a boolean.
Asynchronous version of isOkAnd.
A function that takes the Ok value and returns a PromiseLike<boolean> or boolean.
A promise that resolves to true if the Result is Ok and the predicate resolves to true.
Maps a Result<T, E> to U by applying a function to the contained Ok value (if Ok), or returns the provided default (if Err).
The type of the value returned by the map function or the default value.
Maps a Result<T, E> to U by applying a function to the contained Ok value (if Ok), or computes a default (if Err).
The type of the value returned by the map function or the default function.
Asynchronous version of orElse.
The type of the error returned by the function.
A promise that resolves to this as Ok if this is Ok, otherwise the result of fn.
Non-standard extension: This method is not part of Rust's standard library.
Like orElseAsync, but automatically catches any thrown exceptions or Promise rejections
and converts them to Err.
This is useful when error recovery logic may itself throw, without needing to wrap
the call in tryAsyncResult.
Note: The error type F is preserved. If the function throws, the thrown value
is cast to F. The caller is responsible for ensuring that thrown exceptions
are compatible with type F, similar to how tryAsyncResult<T, E> works.
The type of the error returned by the function.
A promise that resolves to Ok(T) if this is Ok or if fn succeeds, or Err(F) if fn throws/rejects.
const x: Result<number, Error> = Err(new Error('primary failed'));
// If the recovery function succeeds
const result = await x.orTryAsync(async e => {
const response = await fetch('/api/fallback');
return response.json();
});
console.log(result.unwrap()); // { value: 42 }
// If the recovery function throws or rejects
const failed = await x.orTryAsync(async e => {
throw new Error('fallback also failed');
});
console.log(failed.unwrapErr()); // Error: fallback also failed
// If this is already Ok, fn is not called
const y: Result<number, Error> = Ok(10);
const skipped = await y.orTryAsync(async e => 999);
console.log(skipped.unwrap()); // 10
Transposes a Result of an Option into an Option of a Result.
The type of the success value in the Ok variant of the Option.
Some containing Ok if the result is Ok containing Some,
Some containing Err if the result is Err,
None if the result is Ok containing None.
Returns the contained Ok value.
Returns the contained Err value.
The
Resulttype is used for returning and propagating errors. It is an enum with the variants,Ok(T), representing success and containing a value, andErr(E), representing error and containing an error value. This interface includes methods that act on theResulttype, similar to Rust'sResultenum.As Rust Code:
Since
1.0.0
See
https://doc.rust-lang.org/std/result/enum.Result.html