Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite into try-expressions #53

Merged
merged 18 commits into from
Jan 20, 2025
Prev Previous commit
Next Next commit
Update README.md
arthurfiorette authored Jan 20, 2025
commit 2d2e52ddb0faf5e5abd0fd4aa9b160614f3b2071
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -281,21 +281,21 @@ In Go, the convention is to place the data variable first, and you might wonder
If someone is using the `try` expression, it is because they want to ensure they handle errors and avoid neglecting them. Placing the data first would undermine this principle by prioritizing the result over error handling.

```ts
// Ignores errors!
// This line doesn't acknowledge the possibility of errors being thrown
const data = fn()

// It's easy to forget to handle the error
// It's easy to forget to add a second error parameter
const [data] = try fn()

// This is the correct approach
// This approach gives all clues to the reader about the 2 possible states
const [ok, error, data] = try fn()
```

If you want to suppress the error (which is **different** from ignoring the possibility of a function throwing an error), you can do the following:

```ts
// This suppresses the `ok` and `error` (ignores them and doesnt re-throw)
const [,, data] = try fn()
// This suppresses a possible error (Ignores and doesn't re-throw)
const [ok, _, data] = try fn()
```

This approach is explicit and readable, as it acknowledges the possibility of an error while indicating that you do not care about it.