Tries to return value if value is Err
throws custom error message.
Message to show when value is Err
result.expect("Custom message");
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.expect("I failed!")); // "Hello!"
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
result.expect("I failed!"); // Error: I failed!
Tries to return error value if value is Ok
throws custom error message
result.expectErr("Custom message");
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.expectErr("I failed!")); // Error: I failed!
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
console.log(result.expectErr("I failed!")); // "oops!"
Maps Result<T, E>
to Result<A, E>
using the passed mapping function
result.map((val) => val.length);
const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");
const result = functionThatMightFail();
const hello = result.map((val) => val.slice(0, 5));
console.log(hello.unwrap()); // "Hello"
Maps Result<T, E>
to Result<T, A>
using the passed mapping function
result.mapErr((err) => getCodeMsg(err));
const functionThatMightFail = (): Result<string, string> => Err(10);
const result = functionThatMightFail();
const message = result.mapErr(() => "Error");
console.log(message); // "Error"
In the Err
case returns the mapped value using the function else returns defaultVal
result.mapErrOr("Should've been error", (err) => getCodeMsg(err));
Ok
const functionThatMightFail = (): Result<string, string> => Ok("foo");
const result = functionThatMightFail();
const message = result.mapErrOr("Should've been error", () => "Error");
console.log(message); // "Should've been error"
Err
const functionThatMightFail = (): Result<string, string> => Err(10);
const result = functionThatMightFail();
const message = result.mapErrOr("Should've been error", () => "Error");
console.log(message); // "Error"
In the Err
case returns the mapped value using the function else returns value of def
result.mapErrOrElse(() => "Value", (_) => "Error!");
Ok
const functionThatMightFail = (): Result<string, string> => Ok("foo");
const result = functionThatMightFail();
const length = result.mapErrOrElse(() => 1, (val) => val.length);
console.log(length); // 1
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
const length = result.mapOr(() => 1, (val) => val.length);
console.log(length); // 4
In the Ok
case returns the mapped value using the function else returns defaultVal
result.mapOr(1, (val) => val.length);
Ok
const functionThatMightFail = (): Result<string, string> => Ok("foo");
const result = functionThatMightFail();
const length = result.mapOr(1, (val) => val.length);
console.log(length); // 3
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
const length = result.mapOr(1, (val) => val.length);
console.log(length); // 1
In the Ok
case returns the mapped value using fn
else returns value of def
result.mapOrElse(() => 1, (val) => val.length);
Ok
const functionThatMightFail = (): Result<string, string> => Ok("foo");
const result = functionThatMightFail();
const length = result.mapOrElse(() => 1, (val) => val.length);
console.log(length); // 3
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
const length = result.mapOr(() => 1, (val) => val.length);
console.log(length); // 1
Allows you to run callbacks based on the result.
result.match(
(val) => val,
() => {
throw new Error('oops!')
}
);
const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");
const result = functionThatMightFail();
const val = result.match(
(val) => val,
() => {
throw new Error('oops!')
}
);
console.log(val); // "Hello, World!"
Tries to return value if value is Err
throws generic error message.
result.unwrap();
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrap()); // "Hello!"
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
result.unwrap(); // Error: Attempted to call `.unwrap()` on a non `Ok` value.
Tries to return err if value is Ok
throws generic error message.
result.unwrapErr();
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
result.unwrapErr(); // Error: Attempted to call `.unwrapErr()` on a non `Err` value.
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
console.log(result.unwrapErr()); // "oops!"
Tries to unwrap the error if vale is Ok
returns defaultVal
result.unwrapErrOr("Error");
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapErrOr("Yellow!")); // "Yellow!"
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
console.log(result.unwrapErrOr("Yellow!")); // "oops!"
Tries to return the error if value is Ok
calls fn
Function called if Ok
result.unwrapErrOrElse(() => "Error!");
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapErrOrElse(() => "oops!")); // "oops!"
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
console.log(result.unwrapErrOrElse(() => "Hello!")); // "oops!"
Tries to unwrap the value if value is Err
returns defaultVal
Value to be returned if Err
result.unwrapOr(7);
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapOr("Yellow!")); // "Hello!"
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
console.log(result.unwrapOr("Yellow!")); // "Yellow!"
Tries to return the value if value is Err
calls fn
Function called if Err
result.unwrapOrElse(() => "Hello!");
Ok
const functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapOrElse(() => "oops!")); // "Hello!"
Err
const functionThatMightFail = (): Result<string, string> => Err("oops!");
const result = functionThatMightFail();
console.log(result.unwrapOrElse(() => "Hello!")); // "Hello!"
Result allows you to show to a consumer that a function might throw and force them to handle it.
T
Value typeE
Error typeUsage
Examples