Skip to content

Commit 7fcbfe3

Browse files
docs: rename to operator
1 parent 23d2e5c commit 7fcbfe3

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

README.md

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<br />
22

3-
<h1>ECMAScript Try Statements</h1>
3+
<h1>ECMAScript Try Operator</h1>
44

55
> [!WARNING]
6-
> After extensive discussion and feedback, the proposal was renamed from `Safe Assignment Operator` to `Try Statements`. _Click here to view the [original proposal](https://github.com/arthurfiorette/proposal-try-statements/tree/proposal-safe-assignment-operator)._
6+
> After extensive discussion and feedback, the proposal was renamed from `Safe Assignment Operator` to `Try Operator`. _Click here to view the [original proposal](https://github.com/arthurfiorette/proposal-try-operator/tree/proposal-safe-assignment-operator)._
77
88
<br />
99

1010
<div align="center">
11-
<img src="./assets/banner.png" alt="ECMAScript Try Statements Proposal" />
11+
<img src="./assets/banner.png" alt="ECMAScript Try Operator Proposal" />
1212
</div>
1313

1414
<br />
@@ -22,10 +22,10 @@ Only the `catch (error) {}` block represents actual control flow, while no progr
2222

2323
- [Try/Catch Is Not Enough](#trycatch-is-not-enough)
2424
- [What This Proposal Does Not Aim to Solve](#what-this-proposal-does-not-aim-to-solve)
25-
- [Try Statement](#try-statement)
26-
- [Cannot be inlined.](#cannot-be-inlined)
25+
- [Try Operator](#try-operator)
2726
- [Expressions are evaluated in a self-contained `try/catch` block](#expressions-are-evaluated-in-a-self-contained-trycatch-block)
28-
- [Any valid expression can be use](#any-valid-expression-can-be-use)
27+
- [Can be inlined.](#can-be-inlined)
28+
- [Any valid expression can be used](#any-valid-expression-can-be-used)
2929
- [`await` is not an exception](#await-is-not-an-exception)
3030
- [Statements are not expressions](#statements-are-not-expressions)
3131
- [Never throws](#never-throws)
@@ -132,37 +132,58 @@ A `try` statement provide significant flexibility and arguably result in more re
132132

133133
<br />
134134

135-
## Try Statement
135+
## Try Operator
136136

137-
The `try` operator consists of the `try` keyword followed by an expression. Its result is an instance of the [`Result`](#result-class).
137+
The `try` operator consists of the `try` keyword followed by an expression. It results in an instance of the [`Result`](#result-class).
138138

139-
### Cannot be inlined.
139+
<details>
140140

141-
Similar to `throw`, `return`, and `await`
141+
<summary>
142+
All of its usages are just a combination of the above said rules.
143+
</summary>
142144

143145
```js
144-
array.map((fn) => try fn()).filter((result) => result.ok) // Syntax error!
146+
const a = try something()
147+
const [[ok, err, val]] = [try something()]
148+
const [ok, err, val] = try something()
149+
array.map(fn => try fn())
150+
yield try something()
151+
try yield something()
152+
try await something()
153+
try (a instanceof b)
154+
(try a) instanceof Result
155+
const a = try try try try try try 1
145156
```
146157

158+
</details>
159+
147160
### Expressions are evaluated in a self-contained `try/catch` block
148161

149162
```js
150-
const result = try expr1
163+
const result = try expression
151164
```
152165

153166
This is "equivalent" to:
154167

155168
```js
156169
let _result
157170
try {
158-
_result = Result.ok(expr1)
171+
_result = Result.ok(expression)
159172
} catch (error) {
160173
_result = Result.error(error)
161174
}
162175
const result = _result
163176
```
164177

165-
### Any valid expression can be use
178+
### Can be inlined.
179+
180+
Similar to `void`, `typeof`, `yield` and `new`:
181+
182+
```js
183+
array.map((fn) => try fn()).filter((result) => result.ok) // works :)
184+
```
185+
186+
### Any valid expression can be used
166187

167188
```js
168189
const result = try data?.someProperty.anotherFunction?.(await someData()).andAnotherOne()
@@ -182,8 +203,6 @@ try {
182203
const result = _result
183204
```
184205
185-
`try` cannot nest since its a statement.
186-
187206
#### `await` is not an exception
188207
189208
```js
@@ -221,7 +240,7 @@ try {
221240
const result = _result
222241
```
223242
224-
A detailed discussion about this topic is available at [GitHub Issue #54](https://github.com/arthurfiorette/proposal-try-statements/issues/54) for those interested.
243+
A detailed discussion about this topic is available at [GitHub Issue #54](https://github.com/arthurfiorette/proposal-try-operator/issues/54) for those interested.
225244
226245
### Never throws
227246
@@ -238,7 +257,7 @@ Regardless of the type of error that might occur, `try` will catch it. For examp
238257
- If accessing the `thing` property on `some` throws an error.
239258
- Any other exception that can arise on that line of code.
240259
241-
All potential errors are safely caught and encapsulated within the `try` statements.
260+
All potential errors are safely caught and encapsulated within the `try` operator expression.
242261
243262
### Parenthesis Required for Object Literals
244263
@@ -256,7 +275,7 @@ This behavior mirrors how JavaScript differentiates blocks and object literals:
256275
({ a: 1 }) // object with a key `a` and a number `1`
257276
```
258277
259-
A detailed discussion about this topic is available at [GitHub Issue #55](https://github.com/arthurfiorette/proposal-try-statements/issues/55) for those interested.
278+
A detailed discussion about this topic is available at [GitHub Issue #55](https://github.com/arthurfiorette/proposal-try-operator/issues/55) for those interested.
260279
261280
### Void Operations
262281
@@ -370,7 +389,7 @@ try {
370389
}
371390
```
372391
373-
A detailed discussion about this topic is available at [GitHub Issue #13](https://github.com/arthurfiorette/proposal-try-statements/issues/13) for those interested.
392+
A detailed discussion about this topic is available at [GitHub Issue #13](https://github.com/arthurfiorette/proposal-try-operator/issues/13) for those interested.
374393
375394
<br />
376395
@@ -400,7 +419,7 @@ There is no guarantee that `createException` always returns an exception. Someon
400419
401420
Even though such cases are uncommon, they can occur. The `ok` value is crucial to mitigate these runtime risks effectively.
402421
403-
For a more in-depth explanation of this decision, refer to [GitHub Issue #30](https://github.com/arthurfiorette/proposal-try-statements/issues/30).
422+
For a more in-depth explanation of this decision, refer to [GitHub Issue #30](https://github.com/arthurfiorette/proposal-try-operator/issues/30).
404423
405424
<br />
406425

0 commit comments

Comments
 (0)