Interface: IResult<T, E>
A type used to express computations that can fail, it can be used for returning and propagating errors. This is a
type union with the variants Ok(T), representing success and containing a value, and Err(E), representing error
and containing an error value.
Typeparam
T The result's type.
Typeparam
E The error's type.
See
https://doc.rust-lang.org/std/result/index.html
Type parameters
| Type parameter |
|---|
T |
E |
Methods
[iterator]()
[iterator]():
Generator<T,any,unknown>
Returns an iterator over the possibly contained value.
The iterator yields one value if the result is Ok, otherwise none.
Returns
Generator<T, any, unknown>
Examples
const x = ok(7);
for (const value of x) {
console.log(value);
}
// Logs 7
const x = err('Nothing!');
for (const value of x) {
console.log(value);
}
// Doesn't log
See
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:863
and()
Returns result if the result is Ok, otherwise returns the Err value of itself.
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
result | Result<U, E> | The result to check. |
Returns
Result<U, E>
Examples
const x: Result<number, string> = ok(2);
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Late error'));
const x: Result<number, string> = err('Early error');
const y: Result<string, string> = err('Late error');
assert.equal(x.and(y), err('Early error'));
const x: Result<number, string> = ok(2);
const y: Result<string, string> = ok('Hello');
assert.equal(x.and(y), ok('Hello'));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.and
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:601
andThen()
Calls cb if the result is Ok, otherwise returns the Err value of self.
This function can be used for control flow based on Result values.
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => Result<U, E> | The predicate. |
Returns
Result<U, E>
Example
function fractionOf4(value: number) {
return value === 0 ? err('overflowed') : ok(4 / value);
}
assert.equal(ok(2).andThen(fractionOf4), ok(4));
assert.equal(ok(0).andThen(fractionOf4), err('overflowed'));
assert.equal(err('not a number').andThen(fractionOf4), err('not a number'));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:622
contains()
contains(
value:T):boolean
Returns true if the result is an Ok and the given value strict equals it.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | T | The value to compare. |
Returns
boolean
Examples
const x: Result<number, string> = ok(2);
assert.equal(x.contains(2), true);
const x: Result<number, string> = ok(3);
assert.equal(x.contains(2), false);
const x: Result<number, string> = err('Some error message');
assert.equal(x.contains(2), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.contains
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:703
containsErr()
containsErr(
error:E):boolean
Returns true if the result is an Err and the given error strict equals it.
Parameters
| Parameter | Type | Description |
|---|---|---|
error | E | The error to compare. |
Returns
boolean
Examples
const x: Result<number, string> = ok(2);
assert.equal(x.containsErr('Some error message'), false);
const x: Result<number, string> = err('Some error message');
assert.equal(x.containsErr('Some error message'), true);
const x: Result<number, string> = err('Some other error message');
assert.equal(x.containsErr('Some error message'), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.contains_err
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:727
eq()
eq(
other:Result<T,E>):boolean
Checks whether or not other equals with self.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Result<T, E> | The other result to compare. |
Returns
boolean
See
https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:805
err()
err():
Option<E>
Converts from Result<T, E> to Option<E>.
Converts itself into an Option<E>, and discarding the successful value, if any.
Returns
Option<E>
Examples
const x: Result<number, string> = ok(2);
assert.equal(x.err(), none);
const x: Result<number, string> = err('Some error message');
assert.equal(x.err(), 'Some error message');
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.err
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:139
expect()
expect(
message:string):T
Returns the contained Ok value.
If the value is an Err, it throws a ResultError with the given message and the content of the Err.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | The message for the error. |
Returns
T
Examples
const x = ok(2);
assert.equal(x.expect('Whoops!'), 2);
const x = err('Emergency failure');
assert.throws(() => x.expect('Whoops!'), {
name: 'ResultError',
message: 'Whoops',
value: 'Emergency failure'
});
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.expect
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:419
expectErr()
expectErr(
message:string):E
Returns the contained Err value.
If the value is an Ok, it throws a ResultError with the given message and the content of the Ok.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | The message for the error. |
Returns
E
Examples
const x = ok(2);
assert.throws(() => x.expectErr('Whoops!'), {
name: 'ResultError',
message: 'Whoops',
value: 2
});
const x = err('Emergency failure');
assert.equal(x.expectErr('Whoops!'), 'Emergency failure');
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:444
flatten()
Converts from Result<Result<T, E>, E> to Result<T, E>.
Type parameters
| Type parameter |
|---|
IT |
Parameters
| Parameter | Type |
|---|---|
this | Result <Result<IT, E>, E> |
Returns
Result<IT, E>
Examples
const x: Result<Result<string, number>, number> = ok(ok('Hello'));
assert.equal(x.flatten(), ok('Hello'));
const x: Result<Result<string, number>, number> = ok(err(6));
assert.equal(x.flatten(), err(6));
const x: Result<Result<string, number>, number> = err(6);
assert.equal(x.flatten(), err(6));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.flatten
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:766
inspect()
inspect(
cb: (value:T) =>void):this
Calls the provided closure with a reference to the contained value (if Ok).
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => void | The predicate. |
Returns
this
Seealso
inspectAsync for the awaitable version.
Examples
ok(2).inspect(console.log);
// Logs: 2
err('Some error message').inspect(console.log);
// Doesn't log
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:308
inspectAsync()
inspectAsync(
cb: (value:T) =>Awaitable<void>):Promise<IResult<T,E>>
Calls the provided closure with a reference to the contained value (if Ok) and awaits it.
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => Awaitable<void> | The predicate. |
Returns
Seealso
inspect for the sync version.
Examples
await ok(2).inspectAsync(console.log);
// Logs: 2
await err('Some error message').inspectAsync(console.log);
// Doesn't log
Note
This is an extension not supported in Rust
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:328
inspectErr()
inspectErr(
cb: (error:E) =>void):this
Calls the provided closure with a reference to the contained error (if Err).
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (error: E) => void | The predicate. |
Returns
this
Seealso
inspectErrAsync for the awaitable version.
Examples
ok(2).inspectErr(console.log);
// Doesn't log
err('Some error message').inspectErr(console.log);
// Logs: Some error message
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.inspect_err
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:348
inspectErrAsync()
inspectErrAsync(
cb: (error:E) =>Awaitable<void>):Promise<IResult<T,E>>
Calls the provided closure with a reference to the contained error (if Err) and awaits it.
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (error: E) => Awaitable<void> | The predicate. |
Returns
Seealso
inspectErr for the sync version.
Examples
await ok(2).inspectErrAsync(console.log);
// Doesn't log
await err('Some error message').inspectErrAsync(console.log);
// Logs: Some error message
Note
This is an extension not supported in Rust
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:368
intoOkOrErr()
intoOkOrErr():
T|E
Returns the Ok value if self is Ok, and the Err value if self is Err.
Returns
T | E
Examples
let x: Result<number, number> = ok(3);
assert.equal(x.intoOkOrErr(), 3);
let x: Result<number, number> = err(4);
assert.equal(x.intoOkOrErr(), 4);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.into_ok_or_err
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:784
intoPromise()
Returns a Promise object with the awaited value (if Ok) or the awaited error (if Err).
Returns
Promise <Result <Awaited<T>, Awaited<E>>>
Example
let x = ok(Promise.resolve(3));
assert.equal(await x.intoPromise(), ok(3));
Note
This is an extension not supported in Rust
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:797
isErr()
isErr():
this is Err<E>
Returns true if the result is Err.
Returns
this is Err<E>
Examples
const x = ok(-3);
assert.equal(x.isErr(), false);
const x = err('Some error message');
assert.equal(x.isErr(), true);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:75
isErrAnd()
isErrAnd<
R>(cb: (error:E) =>R):this is Err<E> & R
Returns true if the result is Err and the value inside of it matches a predicate.
Type parameters
| Type parameter |
|---|
R extends boolean |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (error: E) => R | The predicate. |
Returns
this is Err<E> & R
Examples
const x = ok(2);
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
const x = err(new Error('Some error message'));
assert.equal(x.isErrAnd((error) => error instanceof TypeError), false);
const x = err(new TypeError('Some error message'));
assert.equal(x.isErrAnd((error) => error instanceof TypeError), true);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err_and
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:99
isOk()
isOk():
this is Ok<T>
Returns true if the result is Ok.
Returns
this is Ok<T>
Examples
const x = ok(-3);
assert.equal(x.isOk(), true);
const x = err('Some error message');
assert.equal(x.isOk(), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:34
isOkAnd()
isOkAnd<
R>(cb: (value:T) =>R):this is Ok<T> & R
Returns true if the result is Ok and the value inside of it matches a predicate.
Type parameters
| Type parameter |
|---|
R extends boolean |
Parameters
| Parameter | Type |
|---|---|
cb | (value: T) => R |
Returns
this is Ok<T> & R
Examples
const x = ok(2);
assert.equal(x.isOkAnd((value) => value > 1), true);
const x = ok(0);
assert.equal(x.isOkAnd((value) => value > 1), false);
const x = err('Some error message');
assert.equal(x.isOkAnd((value) => value > 1), false);
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.is_ok_and
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:57
iter()
iter():
Generator<T,any,unknown>
Returns an iterator over the possibly contained value.
The iterator yields one value if the result is Ok, otherwise none.
Returns
Generator<T, any, unknown>
Examples
const x = ok(7);
for (const value of x.iter()) {
console.log(value);
}
// Logs 7
const x = err('Nothing!');
for (const value of x.iter()) {
console.log(value);
}
// Doesn't log
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.iter
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:394
map()
map<
U>(cb: (value:T) =>U):Result<U,E>
Maps a Result<T, E> to Result<U, E> by applying a function to a contained Ok value, leaving an Err value
untouched.
Type parameters
| Type parameter |
|---|
U |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (value: T) => U | The predicate. |
Returns
Result<U, E>
Examples
const x: Result<number, string> = ok(2);
assert.equal(x.map((value) => value * 2), ok(4));
const x: Result<number, string> = err('Some error message');
assert.equal(x.map((value) => value * 2), err('Some error message'));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.map
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:159
mapErr()
mapErr<
F>(cb: (error:E) =>F):Result<T,F>
Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value
untouched.
This function can be used to pass through a successful result while handling an error.
Type parameters
| Type parameter |
|---|
F |
Parameters
| Parameter | Type | Description |
|---|---|---|
cb | (error: E) => F | The predicate. |
Returns
Result<T, F>
Examples
const x: Result<number, Error> = ok(2);
assert.equal(x.mapErr((error) => error.message), ok(2));
const x: Result<number, Error> = err(new Error('Some error message'));
assert.equal(x.mapErr((error) => error.message), err('Some error message'));
See
https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err
Source
projects/utilities/packages/result/src/lib/Result/IResult.ts:258
mapErrInto()
mapErrInto<
IT,IE>(cb: (error:E) =>Result<IT,IE>):Result<T|IT,IE>
Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value
untouched.
This function can be used to pass through a successful result while handling an error.
Unlike mapErr, this method does not wrap the returned value inside Err, but instead, it returns the
returned value.