Skip to content

Commit cc8fc24

Browse files
try static method
1 parent a651c37 commit cc8fc24

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Only the `catch (error) {}` block represents actual control flow, while no progr
3939
- [Instance Structure](#instance-structure)
4040
- [Iterable Protocol](#iterable-protocol)
4141
- [Manual Creation](#manual-creation)
42+
- [`try()` static method](#try-static-method)
4243
- [No Result Flattening](#no-result-flattening)
4344
- [What This Proposal Does Not Aim to Solve](#what-this-proposal-does-not-aim-to-solve)
4445
- [Type-Safe Errors](#type-safe-errors)
@@ -440,6 +441,12 @@ const success = Result.ok(42)
440441
const failure = Result.error(new Error("Operation failed"))
441442
```
442443
444+
### `try()` static method
445+
446+
It also includes a static `Result.try()` method, which serves as the runtime foundation for the `try` operator. This method wraps a function call, catching any synchronous exceptions or asynchronous rejections and returning a `Result` or `Promise<Result>`, respectively.
447+
448+
The proposed `try expression` syntax is essentially an ergonomic improvement over the more verbose `Result.try(() => expression)`, removing the need for a function wrapper.
449+
443450
### No Result Flattening
444451
445452
The `try` operator and `Result` constructors wrap the value they are given without inspection. If this value is itself a `Result` instance, it will be nested, not flattened. This ensures predictable and consistent behavior.

0 commit comments

Comments
 (0)