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

reasons #1

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,42 @@ if (err) {
}
```

## Why Arrow Functions?

You might wonder why we use `go(() => someFunc())` instead of `go(someFunc())`. Here's why:

1. **Error Capturing**: Using `go(someFunc())` would execute `someFunc` immediately before `go` can catch any errors:
```typescript
// ❌ WRONG: Error throws before go() can catch it
const [result, error] = go(divide(10, 0));

// ✅ CORRECT: Error is properly caught
const [result, error] = go(() => divide(10, 0));
```

2. **Delayed Execution**: The arrow function `() =>` ensures the code runs at the right time:
```typescript
// This allows setup code and multiple operations
const [result, error] = go(() => {
const config = loadConfig(); // setup
return processData(config); // main operation
});
```

3. **Context & Arguments**: Arrow functions let you pass arguments and maintain context:
```typescript
function divide(a: number, b: number) {
if (b === 0) throw new Error("Division by zero");
return a / b;
}

// ❌ INFLEXIBLE: Can't pass arguments
const [result1, error1] = go(divide);

// ✅ CORRECT: Can pass arguments
const [result2, error2] = go(() => divide(10, 2));
```

## Usage Examples

### Basic Error Handling
Expand Down
Loading