For AI agents: the complete documentation index is available at https://massaman.dev/llms.txt, the full documentation bundle is available at https://massaman.dev/llms-full.txt, and this page is available as Markdown at https://massaman.dev/reference/control/unwrapOr.md.

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)

import { err, ok, unwrapOr } from 'massaman/control'

unwrapOr(ok(42), 0)      // 42
unwrapOr(err('bad'), 0)  // 0
unwrapOr(ok(null), 0)    // null
unwrapOr(ok(false), true) // false

This checks the Result's ok discriminant, not the truthiness of its value. Successful null, undefined, false, 0, and empty strings are preserved.

Parameters

resultResult<T, E>required

The result to unwrap.

fallbackUrequired

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:

unwrapOr(ok(42), computeDefault()) // computeDefault runs too.

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