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

unwrap

Extract the value from an Ok result, or throw on Err. Mirrors Rust's Result::unwrap / Result::expect.

const value = unwrap(result)
const value = unwrap(result, 'config required')

Usage

unwrap(result, message?)

unwrap is the escape hatch back to throwing — useful at module boundaries where you can't propagate a Result further (program startup, top-level scripts, test setup). Prefer pattern matching or isOk/isErr guards inside normal code.

import { attempt, unwrap } from 'massaman'

// Boot-time config — failure is fatal, throwing is fine.
const config = unwrap(
  attempt(() => JSON.parse(readFileSync('./config.json', 'utf8'))),
  'config file is missing or invalid',
)

With no message, the original Error from Err is thrown directly. With a message, a new Error is thrown with the original preserved on cause — same shape as Rust's expect.

Parameters

resultResult<T>required

the result to unwrap.

messagestringoptional

a custom error message. Original error becomes cause.

Returns

the unwrapped value.

T

Throws

  • The original Error from Err, if no message is provided.
  • A new Error(message, { cause: original }), if message is provided.

See also