ts-fortress
    Preparing search index...

    Function safeTry

    • Runs a generator body in which yield* Result.safeUnwrap(...) unwraps Ok values and short-circuits on the first Err, emulating Rust's ? operator (early-return error propagation).

      The body must return a Result. If every unwrapped Result is Ok, the returned value is the body's Result; otherwise it is the first Err encountered, and the rest of the body never runs — though the generator is closed rather than dropped, so finally blocks and using disposals pending in the body still run. An exception thrown by the body is not converted to an Err: it propagates out of safeTry (as a rejected Promise for an async body). Use Result.fromThrowable for that.

      Passing an async generator (async function*) returns a Promise<Result<...>> instead, so await can be used inside the body. Note that yield* inside an async function* awaits what it receives, so unwrapping an Ok whose value is itself a Promise yields the awaited value there while the static type still says Promise<...>; keep promises out of the Ok side of a Result unwrapped in an async body.

      Type Parameters

      • Y extends Readonly<{ value: unknown }>

        The union of Err types unwrapped via Result.safeUnwrap (never if nothing is unwrapped).

      • R2 extends UnknownResult

        The Result type returned by the body.

      Parameters

      • body: () => Generator<Y, R2>

        A generator function whose yield* expressions delegate to Result.safeUnwrap and whose return value is a Result.

      Returns Result<UnwrapOk<R2>, UnwrapErr<R2> | UnwrapErr<Y>>

      The body's Result, or the first Err short-circuited out of it (wrapped in a Promise for an async generator body).

      type ParseError = Readonly<{ kind: 'invalid-integer'; input: string }>;

      const parseTwo = (a: string, b: string): Result<number, ParseError> =>
      Result.safeTry(function* () {
      const x = yield* Result.safeUnwrap(SafeNumber.parseInteger(a));

      const y = yield* Result.safeUnwrap(SafeNumber.parseInteger(b));

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

      assert.deepStrictEqual(parseTwo('1', '2'), Result.ok(3));

      assert.isTrue(Result.isErr(parseTwo('1', 'not a number')));
    • Runs a generator body in which yield* Result.safeUnwrap(...) unwraps Ok values and short-circuits on the first Err, emulating Rust's ? operator (early-return error propagation).

      The body must return a Result. If every unwrapped Result is Ok, the returned value is the body's Result; otherwise it is the first Err encountered, and the rest of the body never runs — though the generator is closed rather than dropped, so finally blocks and using disposals pending in the body still run. An exception thrown by the body is not converted to an Err: it propagates out of safeTry (as a rejected Promise for an async body). Use Result.fromThrowable for that.

      Passing an async generator (async function*) returns a Promise<Result<...>> instead, so await can be used inside the body. Note that yield* inside an async function* awaits what it receives, so unwrapping an Ok whose value is itself a Promise yields the awaited value there while the static type still says Promise<...>; keep promises out of the Ok side of a Result unwrapped in an async body.

      Type Parameters

      • Y extends Readonly<{ value: unknown }>

        The union of Err types unwrapped via Result.safeUnwrap (never if nothing is unwrapped).

      • R2 extends UnknownResult

        The Result type returned by the body.

      Parameters

      • body: () => AsyncGenerator<Y, R2>

        A generator function whose yield* expressions delegate to Result.safeUnwrap and whose return value is a Result.

      Returns Promise<Result<UnwrapOk<R2>, UnwrapErr<R2> | UnwrapErr<Y>>>

      The body's Result, or the first Err short-circuited out of it (wrapped in a Promise for an async generator body).

      type ParseError = Readonly<{ kind: 'invalid-integer'; input: string }>;

      const parseTwo = (a: string, b: string): Result<number, ParseError> =>
      Result.safeTry(function* () {
      const x = yield* Result.safeUnwrap(SafeNumber.parseInteger(a));

      const y = yield* Result.safeUnwrap(SafeNumber.parseInteger(b));

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

      assert.deepStrictEqual(parseTwo('1', '2'), Result.ok(3));

      assert.isTrue(Result.isErr(parseTwo('1', 'not a number')));