You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This proposal addresses the ergonomic challenges of managing multiple, often nested, `try/catch` blocks necessary for handling operations that may fail at various points.
23
23
24
-
The try block needlessly encloses the protected code in a block. This often prevents straightforward `const` assignment patterns and can reduce readability and static analysis through additional nesting. The `catch (error) {}` branch is usually where control-flow divergence happens, while the successful path often just assigns a variable.
24
+
The try block needlessly encloses the protected code in a block. This often prevents straightforward `const` assignment patterns and can reduce readability and hinder static analysis through additional nesting. The `catch (error) {}` branch is usually where control-flow divergence happens, while the successful path often just assigns a variable.
25
25
26
26
The solution is to add a `try <expression>` operator, a syntax similar to `await <expression>`, which catches any error that occurs when executing its expression and returns it as a value to the caller.
27
27
@@ -144,7 +144,6 @@ function getPostInfo(session, postSlug, cache, db) {
144
144
145
145
In this example, the `try` blocks introduce additional nesting and prevent the protection a `const` declaration would provide.
146
146
147
-
It also tends to interfere with static analysis tools, forcing developers to look for alternate solutions.
148
147
149
148
Instead, using the proposed `try` operator simplifies the function:
150
149
@@ -291,7 +290,7 @@ function getUser(id, request, response) {
291
290
292
291
This is obviously far more code to maintain, a lot of it repetitive and often redundent in these scenarios.
293
292
294
-
I'm sure there are times when returning a Result might make sense, but this proposal isn't about that. It's about the `try` operator, and the try operator just needs some way to encapsulate its three values.
293
+
There are times when returning a Result might make sense, but this proposal isn't about that. It's about the `try` operator, and the try operator just needs some way to encapsulate its three values.
295
294
296
295
Even here, the benefit of the try operator is that developers can pass the whole try expression to a helper function that converts the Result into their chosen error composition cascade. The callback that would previously have been required, which often makes static analysis more context dependant and difficult, is no longer necessary.
0 commit comments