yield-result 0.1.6 → 0.1.7
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 +40 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -255,6 +255,46 @@ export async function handleOrderCheckout(orderId: string) {
|
|
|
255
255
|
|
|
256
256
|
---
|
|
257
257
|
|
|
258
|
+
## 🚦 Guidelines & Anti-Patterns: Async/Await + Generator Boundaries
|
|
259
|
+
|
|
260
|
+
Combining `async/await` with generator delegation (`yield* await asyncFn()`) is a powerful pattern, but mixing two control-flow models requires adhering to key architectural guidelines to avoid performance pitfalls and concurrency bottlenecks.
|
|
261
|
+
|
|
262
|
+
### 🔴 Anti-Pattern 1: Sequential Request Waterfalls
|
|
263
|
+
|
|
264
|
+
Avoid unwrapping multiple independent async calls sequentially inside `safe.async`:
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
// ❌ ANTI-PATTERN: Creates a sequential waterfall latency bottleneck!
|
|
268
|
+
const user = yield* await fetchUser(id); // Waits 100ms
|
|
269
|
+
const config = yield* await fetchConfig(id); // Waits 100ms (Total: 200ms)
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
**✅ Recommended Pattern (Parallel Concurrency):**
|
|
273
|
+
Execute independent Promises concurrently with `Promise.all` before unwrapping:
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
// ✅ RECOMMENDED: Concurrently fetches in parallel (Total: 100ms)
|
|
277
|
+
const [userRes, configRes] = await Promise.all([fetchUser(id), fetchConfig(id)]);
|
|
278
|
+
const user = yield* userRes;
|
|
279
|
+
const config = yield* configRes;
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
---
|
|
283
|
+
|
|
284
|
+
### 🔴 Anti-Pattern 2: Missing `await` on Async Result Promises
|
|
285
|
+
|
|
286
|
+
Forgetting `await` before yielding an async function that returns `Promise<Result<T, E>>`:
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
// ❌ ANTI-PATTERN: Forgetting 'await' on Promise<Result>
|
|
290
|
+
const user = yield* fetchUserAsync(id); // Error: Trying to iterate over a Promise object!
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
**✅ Recommended Pattern:**
|
|
294
|
+
Always use `yield* await fetchUserAsync(id)` when calling functions that return a `Promise<Result<T, E>>`.
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
258
298
|
## 🛡️ Built-in Safety Features
|
|
259
299
|
|
|
260
300
|
|
package/package.json
CHANGED