ts-fortress
    Preparing search index...

    Function safeUnwrap

    • Unwraps a Result inside a Result.safeTry generator body via yield*, emulating Rust's ? operator.

      If the Result is Ok, the yield* expression evaluates to the contained value and execution continues. If it is Err, the error is yielded to Result.safeTry, which returns it immediately — the rest of the generator body never runs.

      This generator must only be delegated to (yield*) inside a Result.safeTry body; resuming it after it has yielded an Err throws.

      Type Parameters

      • R extends UnknownResult

        The input UnknownResult type.

      Parameters

      • result: R

        The Result to unwrap.

      Returns Generator<NarrowToErr<R>, UnwrapOk<R>, unknown>

      A generator that yields the Err (if any) and returns the Ok value.

      const result = Result.safeTry(function* () {
      const x = yield* Result.safeUnwrap(Result.ok(2));

      const y = yield* Result.safeUnwrap(Result.ok(3));

      return Result.ok(x + y);
      });

      assert.deepStrictEqual(result, Result.ok(5));