safe-await-lib 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -18
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/chelohubinc/safe-await-lib/blob/main/LICENSE)
|
|
6
6
|
|
|
7
7
|
Safe async/await utility for handling promises **without try/catch**.
|
|
8
|
-
Designed for **
|
|
8
|
+
Designed for **JavaScript and TypeScript environments**.
|
|
9
9
|
|
|
10
10
|
---
|
|
11
11
|
|
|
@@ -21,17 +21,17 @@ Traditional `async / await` requires repetitive `try/catch` blocks, which:
|
|
|
21
21
|
|
|
22
22
|
### Core idea
|
|
23
23
|
|
|
24
|
-
> **Async code should never throw — it should return.**
|
|
24
|
+
> **Async code should never throw — it should return a predictable tuple.**
|
|
25
25
|
|
|
26
26
|
---
|
|
27
27
|
|
|
28
|
-
## 🚀 Features (v0.2.
|
|
28
|
+
## 🚀 Features (v0.2.1)
|
|
29
29
|
|
|
30
|
-
- Safe execution of async functions
|
|
30
|
+
- Safe execution of async functions, promises, or synchronous values
|
|
31
31
|
- Always returns a predictable tuple: `[SafeError | null, T | null]`
|
|
32
32
|
- Standardized error normalization
|
|
33
33
|
- Fully typed with TypeScript
|
|
34
|
-
- Compatible with
|
|
34
|
+
- Compatible with all JavaScript and TypeScript environments
|
|
35
35
|
- Dual output: **ESM + CJS**
|
|
36
36
|
- Tree-shakable
|
|
37
37
|
- Built-in utilities:
|
|
@@ -64,19 +64,20 @@ async function fetchData() {
|
|
|
64
64
|
return 'Hello World';
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
// Using a function
|
|
68
|
+
const [err, data] = await safe(() => fetchData());
|
|
68
69
|
|
|
69
70
|
if (err) {
|
|
70
71
|
console.error(err.message, err.code);
|
|
71
72
|
} else {
|
|
72
73
|
console.log(data); // "Hello World"
|
|
73
74
|
}
|
|
74
|
-
```
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
// Using a direct promise
|
|
77
|
+
const [err2, result] = await safe(Promise.resolve(42));
|
|
77
78
|
|
|
78
|
-
|
|
79
|
-
const [
|
|
79
|
+
// Using a synchronous value
|
|
80
|
+
const [err3, value] = await safe(10);
|
|
80
81
|
```
|
|
81
82
|
|
|
82
83
|
---
|
|
@@ -86,10 +87,21 @@ const [err, result] = await safe(Promise.resolve(42));
|
|
|
86
87
|
### `safe(input)`
|
|
87
88
|
|
|
88
89
|
```ts
|
|
89
|
-
safe<T>(input: Promise<T> | (() => T | Promise<T>))
|
|
90
|
+
safe<T>(input: Promise<T> | T | (() => T | Promise<T>))
|
|
90
91
|
→ Promise<[SafeError | null, T | null]>
|
|
91
92
|
```
|
|
92
93
|
|
|
94
|
+
#### Behavior
|
|
95
|
+
|
|
96
|
+
- Never throws — always returns `[SafeError | null, T | null]`
|
|
97
|
+
- Normalizes any thrown or rejected value into a `SafeError`
|
|
98
|
+
- Supports promises, synchronous values, or functions returning a value or promise
|
|
99
|
+
|
|
100
|
+
#### Return
|
|
101
|
+
|
|
102
|
+
- `[null, result]` on success
|
|
103
|
+
- `[SafeError, null]` on failure — use `err.code` to identify the error type
|
|
104
|
+
|
|
93
105
|
---
|
|
94
106
|
|
|
95
107
|
### `SafeError` structure
|
|
@@ -104,7 +116,7 @@ interface SafeError {
|
|
|
104
116
|
|
|
105
117
|
---
|
|
106
118
|
|
|
107
|
-
## 🌱 Advanced Usage (v0.2.
|
|
119
|
+
## 🌱 Advanced Usage (v0.2.1)
|
|
108
120
|
|
|
109
121
|
### ⏱️ Timeout handling — `safe.withTimeout`
|
|
110
122
|
|
|
@@ -116,6 +128,13 @@ if (err?.code === 'TIMEOUT_ERROR') {
|
|
|
116
128
|
}
|
|
117
129
|
```
|
|
118
130
|
|
|
131
|
+
#### Behavior
|
|
132
|
+
|
|
133
|
+
- Resolves with `[null, result]` if completed within the timeout
|
|
134
|
+
- Resolves with `[SafeError, null]` if the operation throws or exceeds the timeout
|
|
135
|
+
- Never throws — always returns `[SafeError | null, T | null]`
|
|
136
|
+
- Use `err?.code === 'TIMEOUT_ERROR'` to detect timeout errors
|
|
137
|
+
|
|
119
138
|
---
|
|
120
139
|
|
|
121
140
|
### 🔁 Retry logic — `safe.retry`
|
|
@@ -137,17 +156,24 @@ if (err) {
|
|
|
137
156
|
}
|
|
138
157
|
```
|
|
139
158
|
|
|
140
|
-
|
|
159
|
+
#### Behavior
|
|
141
160
|
|
|
142
|
-
|
|
161
|
+
- Tries the operation up to `retries` times
|
|
162
|
+
- Optional delay between attempts
|
|
163
|
+
- Invokes `onRetry` after each failed attempt
|
|
164
|
+
- Never throws — always returns `[SafeError | null, T | null]`
|
|
165
|
+
- Returns a `SafeError` with code `RETRY_FAILED` if all attempts fail
|
|
166
|
+
- Use `err?.code === 'RETRY_FAILED'` to detect a complete retry failure
|
|
167
|
+
|
|
168
|
+
---
|
|
143
169
|
|
|
144
|
-
|
|
170
|
+
## ✅ Available Today (v0.2.1)
|
|
145
171
|
|
|
146
172
|
- `safe()` — core safe async execution
|
|
147
173
|
- `safe.withTimeout()` — timeout control
|
|
148
174
|
- `safe.retry()` — retry mechanism
|
|
149
175
|
|
|
150
|
-
More utilities will be added incrementally.
|
|
176
|
+
More utilities will be added incrementally according to the roadmap.
|
|
151
177
|
|
|
152
178
|
---
|
|
153
179
|
|
|
@@ -176,7 +202,7 @@ npm run build
|
|
|
176
202
|
|
|
177
203
|
| Version | Features |
|
|
178
204
|
| ------: | ---------------------- |
|
|
179
|
-
| v0.2.0 | withTimeout, retry ✅
|
|
205
|
+
| v0.2.0 | withTimeout, retry ✅ |
|
|
180
206
|
| v0.3.0 | all, allSettled |
|
|
181
207
|
| v0.4.0 | withContext |
|
|
182
208
|
| v0.5.0 | once |
|
|
@@ -207,7 +233,7 @@ Please respect the existing **TypeScript typings** and **error model**.
|
|
|
207
233
|
|
|
208
234
|
It enforces explicit error handling and removes runtime surprises.
|
|
209
235
|
|
|
210
|
-
### Can I use this in
|
|
236
|
+
### Can I use this in browser, Node.js, or other JS environments?
|
|
211
237
|
|
|
212
238
|
Yes. SAFE-AWAIT-LIB is runtime-agnostic and fully compatible.
|
|
213
239
|
|