This function behaves similarly to map and mapErr except that in this function, func
returns a Result. This means flatMap doesn't auto-wrap the return value from func
while map and mapErr both do.
const square = x => Result.ok(x * x);
const error = () => Result.err('it broke!');
const two = Result.ok(2);
const sixteen = two.flatMap(square).flatMap(square);
// sixteen.isOk() === true
// sixteen.unwrap() === 16
const parseError = Result.err('parsing error');
const result = parseError.flatMap(square).flatMap(square);
// result.isOk() === false
// result.unwrap() === 'parsing error'
const resultAgain = two.flatMap(error).flatMap(square);
// resultAgain.isOk() === false
// resultAgain.unwrap() === 'it broke!'
This function is sometimes called andThen in other libraries.
An Err if this Result is an Err; otherwise passes this Result's
inner value to func and returns the resulting Result.
Tries to return the internal Err value of this Result. Returns a Some containing the value if it is an Err, returns a None if it is an Ok.
You can also think of this function as transforming a Result into an Optional, where the Some comes from the Err and the Ok (if it exists) is thrown away.
const one = Result.err('parsing error');
const maybeOne = one.getErr();
// maybeOne.isSome() === true
// maybeOne.unwrap() === 'parsing error'
const one = Result.ok(1);
const maybeOne = one.getErr();
// maybeOne.isNone() === true
A Some containing this Result's value if it is an Err, otherwise returns a None.
Tries to return the internal Ok value of this Result. Returns a Some containing the value if it is an Ok, returns a None if it is an Err.
You can also think of this function as transforming a Result into an Optional, where the Some comes from the Ok and the Err (if it exists) is thrown away.
const one = Result.ok(1);
const maybeOne = one.getOk();
// maybeOne.isSome() === true
// maybeOne.unwrap() === 1
const one = Result.err('parsing error');
const maybeOne = one.getOk();
// maybeOne.isNone() === true
A Some containing this Result's value if it is an Ok, otherwise returns a None.
If this Result is an Ok, the returned value will be the return of func wrapped in a
new Result (resulting in a new Ok); otherwise the returned value will be a new
Err.
const maybeOne = Result.ok(1);
const maybeTwo = maybeOne.map(x => x * 2);
// maybeTwo.isOk() === true
// maybeTwo.unwrap() === 2
const maybeThree = Result.err(1);
const maybeSix = maybeThree.map(x => x * 2);
// maybeSix.isErr() === true
// maybeSix.unwrapErr() === 1
A function to apply to this Result's contained value.
The return value of func wrapped up as a new Result.
If this Result is an Err, the returned value will be the return of func wrapped in
a new Result (resulting in a new Err); otherwise the returned value will be a new
Ok.
const maybeOne = Result.ok(1);
const maybeTwo = maybeOne.mapErr(x => x * 2);
// maybeTwo.isOk() === true
// maybeTwo.unwrap() === 1
const maybeThree = Result.err(2);
const maybeSix = maybeThree.mapErr(x => x * 2);
// maybeSix.isErr() === true
// maybeSix.unwrapErr() === 4
A function to apply to this Result's contained value.
The return value of func wrapped up as a new Result.
Calls the appropriate function in options and returns the result. If this Result is
an Ok, options.ok is called; otherwise options.err is called.
const maybeOne = Result.ok(1);
const doubled = maybeOne.match({
ok: (val) => val * 2, // double it
err: (err) => 0, // we'll pretend an Err implies a 0
});
// doubled === 2
const maybeTwo = Result.err(2);
const tripled = maybeTwo.match({
ok: (val) => val * 3,
err: (err) => 0,
});
// tripled === 0
See ResultMatch for more details on the structure allowed.
An object adhering to the ResultMatch interface.
The return value from whichever function specified in options is called.
const okay = Result.ok(1);
const error = Result.err('parseError');
const either = okay.orElse(() => error);
// either.isOk() === true
// either.unwrap() === 1
const eitherAgain = error.orElse(() => okay);
// eitherAgain.isOk() === true
// eitherAgain.unwrap() === 1
The argument func will not be evaluated unless this Result is an Err. This
means orElse is ideal for cases when you need to fall back on a value that needs to
be computed (and may be expensive to compute).
this Result if it is an Ok, otherwise func's return value is returned.
const one = Result.ok(1);
// one.toString() === "Ok( 1 )"
const two = Result.err(2);
// two.toString() === "Err( 2 )"
A string representation of this Result.
Returns the value contained by this Result if it is an Ok. Throws an error containing
message if it is an Err or a default message if none is provided.
const maybeOne = Result.ok(1);
// this won't throw because it's an `Ok` value.
const one = maybeOne.unwrap("couldn't unwrap an Ok");
// but:
const maybeOne = Result.err('parsing error');
// this will throw because it's an `Err` value.
const one = maybeOne.unwrap("couldn't unwrap an Ok");
Returns the value contained by this Result if it is an Err. Throws an error
containing message if it is an Ok or a default message if none is provided.
const maybeError = Result.ok(1);
// this will throw because it's an `Ok` value.
const error = maybeError.unwrapErr("couldn't unwrap an Err");
// but:
const maybeError = Result.err('parsing error');
// this won't throw because it's an `Err` value.
const error = maybeError.unwrapErr("couldn't unwrap an Err");
Returns the value contained by this Result if it is an Ok, otherwise returns other
as a default value.
const maybeOne = Result.ok(1);
const one = maybeOne.unwrapOr(2);
// one === 1
const maybeOne = Result.err('parse error');
const one = maybeOne.unwrapOr(2);
// one === 2
The value in this Result if it is an Ok, otherwise returns other.
Returns the value contained by this Result if it is an Ok, otherwise calls func
with the Err value and returns the result.
const maybeOne = Result.ok(1);
const one = maybeOne.unwrapOrElse((err) => err.length);
// one === 1
const maybeOne = Result.err('parse error');
const one = maybeOne.unwrapOrElse((err) => err.length);
// one === 11
The argument func will not be evaluated unless this Result is an Err. This
means unwrapOrElse is ideal for cases when you need to fall back on a value that needs to
be computed (and may be expensive to compute).
The value in this Result if it is an Ok, otherwise returns the return value
of func.
Generated using TypeDoc
Result
An abstract class representing the result of some computation (that may have failed). There are only two concrete classes extending this one: Ok and Err.
Ok-values represent a computation result that returned successfully whileErr-values represent a failed computation. Similar to Optional's Some and None, these two types are useful because they share the same API.This means you could have a function that returns a Result and use the value straight away without having to do a bunch of type checking to determine what happened during the computation.
For example:
// with this we can parse an integer out of a string and never have to check for `NaN`s again function safeParseInt(val) { const parsed = Number.parseInt(val, 10); if (Number.isNan(parsed)) { return Result.err(`Failed to parse an integer from the string "${val}".`); } return Result.ok(parsed); } const num = getSomeNumber(); // maybe this returns a string (for some reason) const parsed = safeParseInt(num).unwrapOr(0); // if it parsed properly, we can get the value // but if it didn't we'll just default back to 0 // or if we want to be more explicit (or do more complex things) const parsed = safeParseInt(num).match({ ok: (val) => { return val; }, // this function will be called if we got an `Ok` back err: (e) => { // and this one will be called if we got an `Err` back console.error(e); return computeSomethingElse(); }, });