unwrapOr
Extract the value from an Ok, or return a fallback for an Err. Unlike unwrap, failure does not throw. Mirrors Rust's Result::unwrap_or.
Usage
unwrapOr(result, fallback)
This checks the Result's ok discriminant, not the truthiness of its value. Successful null, undefined, false, 0, and empty strings are preserved.
Parameters
The result to unwrap.
The value to return on failure. It can have a different type from the success value.
Returns
T | U, not a Result. Values are returned by reference. Function values are returned, never invoked.
Eager fallback evaluation
JavaScript evaluates the fallback expression before calling unwrapOr, even on success:
Prefer literals or already-computed defaults. For conditional recovery work, use orElse(result, () => ok(computeDefault())). If evaluating the fallback throws, the exception occurs before unwrapOr runs.
See also
unwrap— throw on failure insteadorElse— perform recovery only on failure- Result & Errors