Skip to content

Commit a9f1eb2

Browse files
authored
try sync returns (#33)
1 parent dc2a35c commit a9f1eb2

8 files changed

Lines changed: 1728 additions & 640 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
dist
3+
.worktrees

.planning/STATE.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ See: .planning/PROJECT.md (updated 2026-01-31)
1212
Phase: 1 of 3 (Core Try Semantics)
1313
Plan: 0 of TBD in current phase
1414
Status: Ready to plan
15-
Last activity: 2026-01-31 - Completed quick task 001: write tests that verify type safety accoriding to the use cases covered in Readme.md
15+
Last activity: 2026-02-01 - Completed quick task 002: stabilize Try typecheck failures
1616

1717
Progress: [░░░░░░░░░░] 0%
1818

1919
## Performance Metrics
2020

2121
**Velocity:**
22-
- Total plans completed: 1
23-
- Average duration: 2m 41s
24-
- Total execution time: 2m 41s
22+
- Total plans completed: 2
23+
- Average duration: 4m 10s
24+
- Total execution time: 8m 19s
2525

2626
**By Phase:**
2727

2828
| Phase | Plans | Total | Avg/Plan |
2929
|-------|-------|-------|----------|
30-
| Quick | 1 | 2m 41s | 2m 41s |
30+
| Quick | 2 | 8m 19s | 4m 10s |
3131

3232
**Recent Trend:**
3333
- Last 5 plans: -
@@ -42,7 +42,8 @@ Progress: [░░░░░░░░░░] 0%
4242
Decisions are logged in PROJECT.md Key Decisions table.
4343
Recent decisions affecting current work:
4444

45-
- None yet.
45+
- Use return-type generics to preserve async/sync inference in Try
46+
- Treat never as non-promise in IfPromise to avoid type collapse
4647

4748
### Pending Todos
4849

@@ -61,9 +62,10 @@ None yet.
6162
| # | Description | Date | Commit | Directory |
6263
|---|-------------|------|--------|-----------|
6364
| 001 | write tests that verify type safety accoriding to the use cases covered in Readme.md | 2026-01-31 | 9595583 | [001-write-tests-that-verify-type-safety-acco](./quick/001-write-tests-that-verify-type-safety-acco/) |
65+
| 002 | stabilize Try typecheck failures for type-safety tests | 2026-02-01 | 793eed1 | [002-tests-are-failing](./quick/002-tests-are-failing/) |
6466

6567
## Session Continuity
6668

67-
Last session: 2026-01-31 14:21 UTC
68-
Stopped at: Completed quick-001 type-safety-tests plan 001
69+
Last session: 2026-02-01 06:36 UTC
70+
Stopped at: Completed quick-002 tests-are-failing plan 01
6971
Resume file: None

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @power-rent/try-catch
22

3+
## 1.1.0
4+
5+
### Minor Changes
6+
7+
- 97e3047: allow running both sync and async functions
8+
39
## 1.0.1
410

511
### Patch Changes

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,20 @@ The library accepts any parameter types as function arguments:
105105

106106
```typescript
107107
// String parameters
108-
const greeting = await new Try(greet, 'Alice', 'Hi').value();
108+
const greeting = new Try(greet, 'Alice', 'Hi').value();
109109

110110
// Number parameters
111-
const sum = await new Try(add, 5, 3).unwrap();
111+
const sum = new Try(add, 5, 3).unwrap();
112112

113113
// Mixed parameter types
114-
const message = await new Try(formatMessage, 123, 'Test message', true).value();
114+
const message = new Try(formatMessage, 123, 'Test message', true).value();
115115

116116
// No parameters
117-
const timestamp = await new Try(getCurrentTime).value();
117+
const timestamp = new Try(getCurrentTime).value();
118118
```
119119

120+
Sync functions return values immediately; async functions require `await`.
121+
120122
### Advanced Usage
121123

122124
```typescript
@@ -233,22 +235,24 @@ Enable debug logging to console. When enabled, errors will be logged to console.
233235

234236
### Execution Methods
235237

236-
#### `.unwrap(): Promise<Awaited<T>>`
238+
#### `.unwrap(): T | Promise<Awaited<T>>`
237239

238240
Execute the function and return the result. Throws the original error if one occurred. Will mask the error message if `.report('custom message')` is called in the chain.
239241

240242
#### `.default<Return>(defaultValue: Return): Try<T, TArgs>`
241243

242244
Set a default value that will be returned by `.value()` when an exception occurs.
243245

244-
#### `.value(): Promise<Awaited<T> | Return | undefined>`
246+
#### `.value(): T | undefined | Promise<Awaited<T> | undefined>`
245247

246248
Execute the function and return the result, the configured default value, or `undefined` if an error occurs.
247249

248-
#### `.error(): Promise<Error | undefined>`
250+
#### `.error(): Error | undefined | Promise<Error | undefined>`
249251

250252
Execute the function and return the error if one occurred, or `undefined` if successful.
251253

254+
Sync functions return values immediately; async functions return Promises.
255+
252256
## Examples
253257

254258
### Different Parameter Types
@@ -258,20 +262,20 @@ Execute the function and return the error if one occurred, or `undefined` if suc
258262
function greet(name: string, greeting: string = 'Hello'): string {
259263
return `${greeting}, ${name}!`;
260264
}
261-
const greeting = await new Try(greet, 'Alice', 'Hi').value();
265+
const greeting = new Try(greet, 'Alice', 'Hi').value();
262266

263267
// Number parameters
264268
function add(a: number, b: number): number {
265269
return a + b;
266270
}
267-
const sum = await new Try(add, 5, 3).value();
271+
const sum = new Try(add, 5, 3).value();
268272

269273
// Mixed parameter types
270274
function formatMessage(id: number, message: string, urgent: boolean): string {
271275
const prefix = urgent ? '[URGENT]' : '[INFO]';
272276
return `${prefix} #${id}: ${message}`;
273277
}
274-
const formatted = await new Try(formatMessage, 123, 'System error', true)
278+
const formatted = new Try(formatMessage, 123, 'System error', true)
275279
.report('Message formatting failed')
276280
.tag('component', 'notification')
277281
.default('Unexpected error')
@@ -281,7 +285,7 @@ const formatted = await new Try(formatMessage, 123, 'System error', true)
281285
function getCurrentTime(): number {
282286
return Date.now();
283287
}
284-
const timestamp = await new Try(getCurrentTime).value();
288+
const timestamp = new Try(getCurrentTime).value();
285289

286290
// Object parameters (key extraction available)
287291
const user = await new Try(fetchUser, { userId: 123, includeProfile: true })

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@power-rent/try-catch",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "A TypeScript utility for simplified async error handling with Sentry integration",
55
"main": "dist/index.js",
66
"module": "dist/esm/index.js",

0 commit comments

Comments
 (0)