Tries to return value if value is Err throws custom error message.
Message to show when value is Err
result.expect("Custom message");
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.expect("I failed!")); // "Hello!"
Errconst 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");
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.expectErr("I failed!")); // Error: I failed!
Errconst 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));
Okconst 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"
Errconst 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!");
Okconst functionThatMightFail = (): Result<string, string> => Ok("foo");
const result = functionThatMightFail();
const length = result.mapErrOrElse(() => 1, (val) => val.length);
console.log(length); // 1
Errconst 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);
Okconst functionThatMightFail = (): Result<string, string> => Ok("foo");
const result = functionThatMightFail();
const length = result.mapOr(1, (val) => val.length);
console.log(length); // 3
Errconst 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);
Okconst functionThatMightFail = (): Result<string, string> => Ok("foo");
const result = functionThatMightFail();
const length = result.mapOrElse(() => 1, (val) => val.length);
console.log(length); // 3
Errconst 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();
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrap()); // "Hello!"
Errconst 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();
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
result.unwrapErr(); // Error: Attempted to call `.unwrapErr()` on a non `Err` value.
Errconst 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");
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapErrOr("Yellow!")); // "Yellow!"
Errconst 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!");
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapErrOrElse(() => "oops!")); // "oops!"
Errconst 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);
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapOr("Yellow!")); // "Hello!"
Errconst 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!");
Okconst functionThatMightFail = (): Result<string, string> => Ok("Hello!");
const result = functionThatMightFail();
console.log(result.unwrapOrElse(() => "oops!")); // "Hello!"
Errconst 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.
TValue typeEError typeUsage
Usage