The union of Err types unwrapped via
Result.safeUnwrap (never if nothing is unwrapped).
The Result type returned by the body.
A generator function whose yield* expressions delegate to
Result.safeUnwrap and whose return value is a Result.
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.
The union of Err types unwrapped via
Result.safeUnwrap (never if nothing is unwrapped).
The Result type returned by the body.
A generator function whose yield* expressions delegate to
Result.safeUnwrap and whose return value is a Result.
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(...)unwrapsOkvalues and short-circuits on the firstErr, emulating Rust's?operator (early-return error propagation).The body must return a
Result. If every unwrappedResultisOk, the returned value is the body'sResult; otherwise it is the firstErrencountered, and the rest of the body never runs — though the generator is closed rather than dropped, sofinallyblocks andusingdisposals pending in the body still run. An exception thrown by the body is not converted to anErr: it propagates out ofsafeTry(as a rejectedPromisefor an async body). Use Result.fromThrowable for that.Passing an async generator (
async function*) returns aPromise<Result<...>>instead, soawaitcan be used inside the body. Note thatyield*inside anasync function*awaits what it receives, so unwrapping anOkwhose value is itself aPromiseyields the awaited value there while the static type still saysPromise<...>; keep promises out of theOkside of aResultunwrapped in an async body.