raw method

  1. @unsafeOrError
FutureOr<Object> raw({
  1. required FutureOr<Object> onErr(
    1. Err<Object> err
    ),
  2. required FutureOr<Object> onNone(),
})

The low-level primitive for reducing a Outcome chain. It recursively unwraps all Outcome layers to return the innermost raw value, forcing the caller to handle terminal states via callbacks.

  • onErr: A function that is called when an Err is encountered.
  • onNone: A function that is called when a None is encountered.

⚠️ Unsafe

Unlike the rest of the package, this primitive does not absorb throws from the onErr / onNone callbacks — they can escape directly to the caller. Prefer rawSync / rawAsync, which wrap this call in a Sync / Async factory that catches any throw into a structured Err. Use UNSAFE(() => …) (or the UNSAFE: label) when calling this method directly, to signal at the call site that error handling has been considered outside the Outcome type system.

Implementation

@unsafeOrError
FutureOr<Object> raw({
  required FutureOr<Object> Function(Err<Object> err) onErr,
  required FutureOr<Object> Function() onNone,
}) {
  FutureOr<Object> dive(Object start) {
    // Iterative peel for all synchronous layers. Recursion is reserved for
    // crossing into a Future via `.then(dive)`, so the call depth is bounded
    // by the number of async hops in the chain, not the total nesting depth.
    var current = start;
    while (true) {
      if (current is Err) {
        return onErr(current);
      }
      if (current is None) {
        return onNone();
      }
      if (current is Outcome) {
        final inner = current.value;
        if (inner is Future<Object>) {
          return inner.then(dive);
        }
        current = inner;
        continue;
      }
      return current;
    }
  }

  return dive(this);
}