ndomo 0.1.0
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/.bun-version +1 -0
- package/.dockerignore +79 -0
- package/.editorconfig +18 -0
- package/.env.example +19 -0
- package/.github/CODEOWNERS +8 -0
- package/.github/ISSUE_TEMPLATE/bug_report.yml +62 -0
- package/.github/ISSUE_TEMPLATE/config.yml +2 -0
- package/.github/ISSUE_TEMPLATE/feature_request.yml +34 -0
- package/.github/dependabot.yml +36 -0
- package/.github/pull_request_template.md +24 -0
- package/.github/release.yml +30 -0
- package/.github/workflows/gitleaks.yml +28 -0
- package/.github/workflows/release-please.yml +27 -0
- package/.github/workflows/smoke.yml +29 -0
- package/.husky/commit-msg +1 -0
- package/CHANGELOG.md +114 -0
- package/Dockerfile +32 -0
- package/README.es.md +174 -0
- package/README.md +187 -0
- package/agents/chronicler.md +98 -0
- package/agents/ci-smith.md +136 -0
- package/agents/craftsman.md +341 -0
- package/agents/deploy-smith.md +138 -0
- package/agents/foreman.md +377 -0
- package/agents/go-smith.md +164 -0
- package/agents/guild.md +188 -0
- package/agents/inspector.md +83 -0
- package/agents/js-smith.md +127 -0
- package/agents/ops-scout.md +173 -0
- package/agents/painter.md +200 -0
- package/agents/python-smith.md +120 -0
- package/agents/ranger.md +307 -0
- package/agents/release-smith.md +165 -0
- package/agents/rust-smith.md +159 -0
- package/agents/sage.md +178 -0
- package/agents/scout.md +144 -0
- package/agents/scribe.md +156 -0
- package/agents/smith.md +201 -0
- package/agents/vue-smith.md +155 -0
- package/agents/warden.md +216 -0
- package/agents/zig-smith.md +156 -0
- package/bin/ndomo-analyses.ts +4 -0
- package/bin/ndomo-status.ts +4 -0
- package/biome.json +57 -0
- package/bun.lock +514 -0
- package/commitlint.config.js +3 -0
- package/config/ndomo.config.json +258 -0
- package/config/ndomo.schema.json +166 -0
- package/docs/agents.md +375 -0
- package/docs/bugs/plan-create-orphan-fk.md +131 -0
- package/docs/bugs/task_create_batch-order-index-collision.md +158 -0
- package/docs/configuration.md +276 -0
- package/docs/database.md +364 -0
- package/docs/features/feature-flexible-builder-v1.md +724 -0
- package/docs/features/feature-flexible-builder-v2.md +882 -0
- package/docs/features/feature-flexible-builder.md +974 -0
- package/docs/http-server.md +244 -0
- package/docs/installation.md +259 -0
- package/docs/integrations.md +129 -0
- package/docs/operations/anti-pattern-sub-agent-verify-2026-06-21.md +32 -0
- package/docs/operations/audit-v1.md +417 -0
- package/docs/operations/audit-v2.md +197 -0
- package/docs/operations/audit-v3.md +306 -0
- package/docs/operations/db-optimize-foundations.md +123 -0
- package/docs/operations/verify-gate-architecture.md +82 -0
- package/docs/workflows.md +448 -0
- package/opencode.json +5 -0
- package/package.json +65 -0
- package/release-please-config.json +11 -0
- package/scripts/dev-bust-cache.sh +164 -0
- package/scripts/install.sh +688 -0
- package/scripts/smoke-e2e.ts +704 -0
- package/scripts/smoke-hot.ts +417 -0
- package/scripts/smoke-http.sh +228 -0
- package/scripts/smoke-v4.ts +256 -0
- package/scripts/smoke-v5.ts +397 -0
- package/scripts/smoke.sh +9 -0
- package/scripts/uninstall.sh +224 -0
- package/skills/api-security-best-practices/SKILL.md +915 -0
- package/skills/bash-scripting/SKILL.md +201 -0
- package/skills/bun/SKILL.md +313 -0
- package/skills/cavecrew/SKILL.md +82 -0
- package/skills/caveman/SKILL.md +74 -0
- package/skills/caveman-review/README.md +33 -0
- package/skills/caveman-review/SKILL.md +55 -0
- package/skills/find-skills/SKILL.md +142 -0
- package/skills/frontend-design/LICENSE.txt +177 -0
- package/skills/frontend-design/SKILL.md +55 -0
- package/skills/golang-patterns/SKILL.md +674 -0
- package/skills/golang-security/SKILL.md +185 -0
- package/skills/golang-security/evals/evals.json +595 -0
- package/skills/golang-security/references/architecture.md +268 -0
- package/skills/golang-security/references/checklist.md +80 -0
- package/skills/golang-security/references/cookies.md +200 -0
- package/skills/golang-security/references/cryptography.md +424 -0
- package/skills/golang-security/references/filesystem.md +285 -0
- package/skills/golang-security/references/injection.md +315 -0
- package/skills/golang-security/references/logging.md +163 -0
- package/skills/golang-security/references/memory-safety.md +241 -0
- package/skills/golang-security/references/network.md +253 -0
- package/skills/golang-security/references/secrets.md +189 -0
- package/skills/golang-security/references/third-party.md +159 -0
- package/skills/golang-security/references/threat-modeling.md +189 -0
- package/skills/golang-testing/SKILL.md +720 -0
- package/skills/grill-me/SKILL.md +7 -0
- package/skills/javascript-testing-patterns/SKILL.md +537 -0
- package/skills/javascript-testing-patterns/references/advanced-testing-patterns.md +513 -0
- package/skills/modern-javascript-patterns/SKILL.md +43 -0
- package/skills/modern-javascript-patterns/references/advanced-patterns.md +487 -0
- package/skills/modern-javascript-patterns/references/details.md +457 -0
- package/skills/python-anti-patterns/SKILL.md +349 -0
- package/skills/python-design-patterns/SKILL.md +85 -0
- package/skills/python-design-patterns/references/details.md +353 -0
- package/skills/python-error-handling/SKILL.md +193 -0
- package/skills/python-error-handling/references/details.md +171 -0
- package/skills/python-testing-patterns/SKILL.md +278 -0
- package/skills/python-testing-patterns/references/advanced-patterns.md +411 -0
- package/skills/python-testing-patterns/references/details.md +349 -0
- package/skills/rust-patterns/SKILL.md +500 -0
- package/skills/rust-testing/SKILL.md +501 -0
- package/skills/security-review/SKILL.md +504 -0
- package/skills/security-review/cloud-infrastructure-security.md +361 -0
- package/skills/vue-best-practices/SKILL.md +154 -0
- package/skills/vue-best-practices/references/animation-class-based-technique.md +254 -0
- package/skills/vue-best-practices/references/animation-state-driven-technique.md +291 -0
- package/skills/vue-best-practices/references/component-async.md +97 -0
- package/skills/vue-best-practices/references/component-data-flow.md +307 -0
- package/skills/vue-best-practices/references/component-fallthrough-attrs.md +174 -0
- package/skills/vue-best-practices/references/component-keep-alive.md +137 -0
- package/skills/vue-best-practices/references/component-slots.md +216 -0
- package/skills/vue-best-practices/references/component-suspense.md +228 -0
- package/skills/vue-best-practices/references/component-teleport.md +108 -0
- package/skills/vue-best-practices/references/component-transition-group.md +128 -0
- package/skills/vue-best-practices/references/component-transition.md +125 -0
- package/skills/vue-best-practices/references/composables.md +290 -0
- package/skills/vue-best-practices/references/directives.md +162 -0
- package/skills/vue-best-practices/references/perf-avoid-component-abstraction-in-lists.md +159 -0
- package/skills/vue-best-practices/references/perf-v-once-v-memo-directives.md +182 -0
- package/skills/vue-best-practices/references/perf-virtualize-large-lists.md +187 -0
- package/skills/vue-best-practices/references/plugins.md +166 -0
- package/skills/vue-best-practices/references/reactivity.md +344 -0
- package/skills/vue-best-practices/references/render-functions.md +201 -0
- package/skills/vue-best-practices/references/sfc.md +310 -0
- package/skills/vue-best-practices/references/state-management.md +135 -0
- package/skills/vue-best-practices/references/updated-hook-performance.md +187 -0
- package/skills/vue-pinia-best-practices/SKILL.md +21 -0
- package/skills/vue-pinia-best-practices/reference/pinia-no-active-pinia-error.md +248 -0
- package/skills/vue-pinia-best-practices/reference/pinia-setup-store-return-all-state.md +227 -0
- package/skills/vue-pinia-best-practices/reference/pinia-store-destructuring-breaks-reactivity.md +193 -0
- package/skills/vue-pinia-best-practices/reference/state-url-for-ephemeral-filters.md +238 -0
- package/skills/vue-pinia-best-practices/reference/state-use-pinia-for-large-apps.md +262 -0
- package/skills/vue-pinia-best-practices/reference/store-method-binding-parentheses.md +191 -0
- package/skills/zig-0.16/SKILL.md +840 -0
- package/skills/zig-0.16/scripts/check-zig-version.sh +21 -0
- package/src/cli/analyses.ts +280 -0
- package/src/cli/index.ts +108 -0
- package/src/cli/serve.ts +192 -0
- package/src/cli/smoke.ts +131 -0
- package/src/cli/status.test.ts +204 -0
- package/src/cli/status.ts +263 -0
- package/src/cli/vacuum.test.ts +82 -0
- package/src/cli/vacuum.ts +96 -0
- package/src/config/schema.test.ts +88 -0
- package/src/config/schema.ts +64 -0
- package/src/db/analyses-migration.test.ts +210 -0
- package/src/db/analyses.test.ts +466 -0
- package/src/db/analyses.ts +375 -0
- package/src/db/auto-checkpoint.ts +131 -0
- package/src/db/client.test.ts +129 -0
- package/src/db/client.ts +55 -0
- package/src/db/fts-escape.ts +20 -0
- package/src/db/incidents.test.ts +201 -0
- package/src/db/incidents.ts +93 -0
- package/src/db/index.ts +86 -0
- package/src/db/migrations-v13.test.ts +141 -0
- package/src/db/migrations-v8.test.ts +301 -0
- package/src/db/migrations.ts +147 -0
- package/src/db/plan-archive.test.ts +180 -0
- package/src/db/plan-archive.ts +274 -0
- package/src/db/plan-create.test.ts +276 -0
- package/src/db/plan-create.ts +78 -0
- package/src/db/plan-files.test.ts +289 -0
- package/src/db/plan-update-status.ts +287 -0
- package/src/db/plans.test.ts +490 -0
- package/src/db/plans.ts +534 -0
- package/src/db/resolve-project-dir.test.ts +143 -0
- package/src/db/resolve-project-dir.ts +75 -0
- package/src/db/rollbacks.test.ts +150 -0
- package/src/db/rollbacks.ts +67 -0
- package/src/db/schema.ts +907 -0
- package/src/db/sessions.test.ts +80 -0
- package/src/db/sessions.ts +135 -0
- package/src/db/shutdown.test.ts +147 -0
- package/src/db/shutdown.ts +45 -0
- package/src/db/tasks.test.ts +921 -0
- package/src/db/tasks.ts +747 -0
- package/src/db/types.ts +619 -0
- package/src/http/__tests__/auth.test.ts +196 -0
- package/src/http/__tests__/routes.test.ts +465 -0
- package/src/http/__tests__/sse.test.ts +317 -0
- package/src/http/auth.ts +72 -0
- package/src/http/middleware/cors.ts +53 -0
- package/src/http/middleware/security-headers.ts +21 -0
- package/src/http/routes/events.ts +112 -0
- package/src/http/routes/health.ts +51 -0
- package/src/http/routes/plans.ts +66 -0
- package/src/http/routes/sessions.ts +50 -0
- package/src/http/routes/tasks.ts +60 -0
- package/src/http/server.ts +95 -0
- package/src/http/sse.ts +116 -0
- package/src/index.ts +37 -0
- package/src/lib.ts +65 -0
- package/src/mem/scoped.ts +65 -0
- package/src/orchestrator/background.test.ts +268 -0
- package/src/orchestrator/background.ts +293 -0
- package/src/orchestrator/memory-hook.ts +182 -0
- package/src/orchestrator/reconciler.ts +123 -0
- package/src/orchestrator/scheduler.test.ts +300 -0
- package/src/orchestrator/scheduler.ts +243 -0
- package/src/plugin.test.ts +2574 -0
- package/src/plugin.ts +1690 -0
- package/src/sdk/client.ts +66 -0
- package/src/worktrees/manager.ts +236 -0
- package/src/worktrees/state.ts +87 -0
- package/tests/integration/ranger-flow.test.ts +257 -0
- package/tools/analysis_archive.ts +28 -0
- package/tools/analysis_create.ts +55 -0
- package/tools/analysis_get.ts +33 -0
- package/tools/analysis_link_plan.ts +44 -0
- package/tools/analysis_list.ts +48 -0
- package/tools/analysis_search.ts +36 -0
- package/tools/analysis_update.ts +44 -0
- package/tools/plan_approve.ts +31 -0
- package/tools/plan_create.ts +58 -0
- package/tools/plan_get.ts +40 -0
- package/tools/plan_list.ts +37 -0
- package/tools/plan_search.ts +34 -0
- package/tools/plan_update_status.ts +71 -0
- package/tools/session_checkpoint.ts +31 -0
- package/tools/session_end.ts +26 -0
- package/tools/session_start.ts +43 -0
- package/tools/task_create_batch.ts +70 -0
- package/tools/task_list.ts +35 -0
- package/tools/task_next_for_agent.ts +30 -0
- package/tools/task_search.ts +34 -0
- package/tools/task_update_status.ts +37 -0
- package/tsconfig.json +31 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
# modern-javascript-patterns — detailed patterns and worked examples
|
|
2
|
+
|
|
3
|
+
## ES6+ Core Features
|
|
4
|
+
|
|
5
|
+
### 1. Arrow Functions
|
|
6
|
+
|
|
7
|
+
**Syntax and Use Cases:**
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
// Traditional function
|
|
11
|
+
function add(a, b) {
|
|
12
|
+
return a + b;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Arrow function
|
|
16
|
+
const add = (a, b) => a + b;
|
|
17
|
+
|
|
18
|
+
// Single parameter (parentheses optional)
|
|
19
|
+
const double = (x) => x * 2;
|
|
20
|
+
|
|
21
|
+
// No parameters
|
|
22
|
+
const getRandom = () => Math.random();
|
|
23
|
+
|
|
24
|
+
// Multiple statements (need curly braces)
|
|
25
|
+
const processUser = (user) => {
|
|
26
|
+
const normalized = user.name.toLowerCase();
|
|
27
|
+
return { ...user, name: normalized };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// Returning objects (wrap in parentheses)
|
|
31
|
+
const createUser = (name, age) => ({ name, age });
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Lexical 'this' Binding:**
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
class Counter {
|
|
38
|
+
constructor() {
|
|
39
|
+
this.count = 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Arrow function preserves 'this' context
|
|
43
|
+
increment = () => {
|
|
44
|
+
this.count++;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Traditional function loses 'this' in callbacks
|
|
48
|
+
incrementTraditional() {
|
|
49
|
+
setTimeout(function () {
|
|
50
|
+
this.count++; // 'this' is undefined
|
|
51
|
+
}, 1000);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Arrow function maintains 'this'
|
|
55
|
+
incrementArrow() {
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
this.count++; // 'this' refers to Counter instance
|
|
58
|
+
}, 1000);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2. Destructuring
|
|
64
|
+
|
|
65
|
+
**Object Destructuring:**
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
const user = {
|
|
69
|
+
id: 1,
|
|
70
|
+
name: "John Doe",
|
|
71
|
+
email: "john@example.com",
|
|
72
|
+
address: {
|
|
73
|
+
city: "New York",
|
|
74
|
+
country: "USA",
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Basic destructuring
|
|
79
|
+
const { name, email } = user;
|
|
80
|
+
|
|
81
|
+
// Rename variables
|
|
82
|
+
const { name: userName, email: userEmail } = user;
|
|
83
|
+
|
|
84
|
+
// Default values
|
|
85
|
+
const { age = 25 } = user;
|
|
86
|
+
|
|
87
|
+
// Nested destructuring
|
|
88
|
+
const {
|
|
89
|
+
address: { city, country },
|
|
90
|
+
} = user;
|
|
91
|
+
|
|
92
|
+
// Rest operator
|
|
93
|
+
const { id, ...userWithoutId } = user;
|
|
94
|
+
|
|
95
|
+
// Function parameters
|
|
96
|
+
function greet({ name, age = 18 }) {
|
|
97
|
+
console.log(`Hello ${name}, you are ${age}`);
|
|
98
|
+
}
|
|
99
|
+
greet(user);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Array Destructuring:**
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
const numbers = [1, 2, 3, 4, 5];
|
|
106
|
+
|
|
107
|
+
// Basic destructuring
|
|
108
|
+
const [first, second] = numbers;
|
|
109
|
+
|
|
110
|
+
// Skip elements
|
|
111
|
+
const [, , third] = numbers;
|
|
112
|
+
|
|
113
|
+
// Rest operator
|
|
114
|
+
const [head, ...tail] = numbers;
|
|
115
|
+
|
|
116
|
+
// Swapping variables
|
|
117
|
+
let a = 1,
|
|
118
|
+
b = 2;
|
|
119
|
+
[a, b] = [b, a];
|
|
120
|
+
|
|
121
|
+
// Function return values
|
|
122
|
+
function getCoordinates() {
|
|
123
|
+
return [10, 20];
|
|
124
|
+
}
|
|
125
|
+
const [x, y] = getCoordinates();
|
|
126
|
+
|
|
127
|
+
// Default values
|
|
128
|
+
const [one, two, three = 0] = [1, 2];
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### 3. Spread and Rest Operators
|
|
132
|
+
|
|
133
|
+
**Spread Operator:**
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
// Array spreading
|
|
137
|
+
const arr1 = [1, 2, 3];
|
|
138
|
+
const arr2 = [4, 5, 6];
|
|
139
|
+
const combined = [...arr1, ...arr2];
|
|
140
|
+
|
|
141
|
+
// Object spreading
|
|
142
|
+
const defaults = { theme: "dark", lang: "en" };
|
|
143
|
+
const userPrefs = { theme: "light" };
|
|
144
|
+
const settings = { ...defaults, ...userPrefs };
|
|
145
|
+
|
|
146
|
+
// Function arguments
|
|
147
|
+
const numbers = [1, 2, 3];
|
|
148
|
+
Math.max(...numbers);
|
|
149
|
+
|
|
150
|
+
// Copying arrays/objects (shallow copy)
|
|
151
|
+
const copy = [...arr1];
|
|
152
|
+
const objCopy = { ...user };
|
|
153
|
+
|
|
154
|
+
// Adding items immutably
|
|
155
|
+
const newArr = [...arr1, 4, 5];
|
|
156
|
+
const newObj = { ...user, age: 30 };
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Rest Parameters:**
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
// Collect function arguments
|
|
163
|
+
function sum(...numbers) {
|
|
164
|
+
return numbers.reduce((total, num) => total + num, 0);
|
|
165
|
+
}
|
|
166
|
+
sum(1, 2, 3, 4, 5);
|
|
167
|
+
|
|
168
|
+
// With regular parameters
|
|
169
|
+
function greet(greeting, ...names) {
|
|
170
|
+
return `${greeting} ${names.join(", ")}`;
|
|
171
|
+
}
|
|
172
|
+
greet("Hello", "John", "Jane", "Bob");
|
|
173
|
+
|
|
174
|
+
// Object rest
|
|
175
|
+
const { id, ...userData } = user;
|
|
176
|
+
|
|
177
|
+
// Array rest
|
|
178
|
+
const [first, ...rest] = [1, 2, 3, 4, 5];
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### 4. Template Literals
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// Basic usage
|
|
185
|
+
const name = "John";
|
|
186
|
+
const greeting = `Hello, ${name}!`;
|
|
187
|
+
|
|
188
|
+
// Multi-line strings
|
|
189
|
+
const html = `
|
|
190
|
+
<div>
|
|
191
|
+
<h1>${title}</h1>
|
|
192
|
+
<p>${content}</p>
|
|
193
|
+
</div>
|
|
194
|
+
`;
|
|
195
|
+
|
|
196
|
+
// Expression evaluation
|
|
197
|
+
const price = 19.99;
|
|
198
|
+
const total = `Total: $${(price * 1.2).toFixed(2)}`;
|
|
199
|
+
|
|
200
|
+
// Tagged template literals
|
|
201
|
+
function highlight(strings, ...values) {
|
|
202
|
+
return strings.reduce((result, str, i) => {
|
|
203
|
+
const value = values[i] || "";
|
|
204
|
+
return result + str + `<mark>${value}</mark>`;
|
|
205
|
+
}, "");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const name = "John";
|
|
209
|
+
const age = 30;
|
|
210
|
+
const html = highlight`Name: ${name}, Age: ${age}`;
|
|
211
|
+
// Output: "Name: <mark>John</mark>, Age: <mark>30</mark>"
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### 5. Enhanced Object Literals
|
|
215
|
+
|
|
216
|
+
```javascript
|
|
217
|
+
const name = "John";
|
|
218
|
+
const age = 30;
|
|
219
|
+
|
|
220
|
+
// Shorthand property names
|
|
221
|
+
const user = { name, age };
|
|
222
|
+
|
|
223
|
+
// Shorthand method names
|
|
224
|
+
const calculator = {
|
|
225
|
+
add(a, b) {
|
|
226
|
+
return a + b;
|
|
227
|
+
},
|
|
228
|
+
subtract(a, b) {
|
|
229
|
+
return a - b;
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// Computed property names
|
|
234
|
+
const field = "email";
|
|
235
|
+
const user = {
|
|
236
|
+
name: "John",
|
|
237
|
+
[field]: "john@example.com",
|
|
238
|
+
[`get${field.charAt(0).toUpperCase()}${field.slice(1)}`]() {
|
|
239
|
+
return this[field];
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// Dynamic property creation
|
|
244
|
+
const createUser = (name, ...props) => {
|
|
245
|
+
return props.reduce(
|
|
246
|
+
(user, [key, value]) => ({
|
|
247
|
+
...user,
|
|
248
|
+
[key]: value,
|
|
249
|
+
}),
|
|
250
|
+
{ name },
|
|
251
|
+
);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const user = createUser("John", ["age", 30], ["email", "john@example.com"]);
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Asynchronous Patterns
|
|
258
|
+
|
|
259
|
+
### 1. Promises
|
|
260
|
+
|
|
261
|
+
**Creating and Using Promises:**
|
|
262
|
+
|
|
263
|
+
```javascript
|
|
264
|
+
// Creating a promise
|
|
265
|
+
const fetchUser = (id) => {
|
|
266
|
+
return new Promise((resolve, reject) => {
|
|
267
|
+
setTimeout(() => {
|
|
268
|
+
if (id > 0) {
|
|
269
|
+
resolve({ id, name: "John" });
|
|
270
|
+
} else {
|
|
271
|
+
reject(new Error("Invalid ID"));
|
|
272
|
+
}
|
|
273
|
+
}, 1000);
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
// Using promises
|
|
278
|
+
fetchUser(1)
|
|
279
|
+
.then((user) => console.log(user))
|
|
280
|
+
.catch((error) => console.error(error))
|
|
281
|
+
.finally(() => console.log("Done"));
|
|
282
|
+
|
|
283
|
+
// Chaining promises
|
|
284
|
+
fetchUser(1)
|
|
285
|
+
.then((user) => fetchUserPosts(user.id))
|
|
286
|
+
.then((posts) => processPosts(posts))
|
|
287
|
+
.then((result) => console.log(result))
|
|
288
|
+
.catch((error) => console.error(error));
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
**Promise Combinators:**
|
|
292
|
+
|
|
293
|
+
```javascript
|
|
294
|
+
// Promise.all - Wait for all promises
|
|
295
|
+
const promises = [fetchUser(1), fetchUser(2), fetchUser(3)];
|
|
296
|
+
|
|
297
|
+
Promise.all(promises)
|
|
298
|
+
.then((users) => console.log(users))
|
|
299
|
+
.catch((error) => console.error("At least one failed:", error));
|
|
300
|
+
|
|
301
|
+
// Promise.allSettled - Wait for all, regardless of outcome
|
|
302
|
+
Promise.allSettled(promises).then((results) => {
|
|
303
|
+
results.forEach((result) => {
|
|
304
|
+
if (result.status === "fulfilled") {
|
|
305
|
+
console.log("Success:", result.value);
|
|
306
|
+
} else {
|
|
307
|
+
console.log("Error:", result.reason);
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
// Promise.race - First to complete
|
|
313
|
+
Promise.race(promises)
|
|
314
|
+
.then((winner) => console.log("First:", winner))
|
|
315
|
+
.catch((error) => console.error(error));
|
|
316
|
+
|
|
317
|
+
// Promise.any - First to succeed
|
|
318
|
+
Promise.any(promises)
|
|
319
|
+
.then((first) => console.log("First success:", first))
|
|
320
|
+
.catch((error) => console.error("All failed:", error));
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
### 2. Async/Await
|
|
324
|
+
|
|
325
|
+
**Basic Usage:**
|
|
326
|
+
|
|
327
|
+
```javascript
|
|
328
|
+
// Async function always returns a Promise
|
|
329
|
+
async function fetchUser(id) {
|
|
330
|
+
const response = await fetch(`/api/users/${id}`);
|
|
331
|
+
const user = await response.json();
|
|
332
|
+
return user;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Error handling with try/catch
|
|
336
|
+
async function getUserData(id) {
|
|
337
|
+
try {
|
|
338
|
+
const user = await fetchUser(id);
|
|
339
|
+
const posts = await fetchUserPosts(user.id);
|
|
340
|
+
return { user, posts };
|
|
341
|
+
} catch (error) {
|
|
342
|
+
console.error("Error fetching data:", error);
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Sequential vs Parallel execution
|
|
348
|
+
async function sequential() {
|
|
349
|
+
const user1 = await fetchUser(1); // Wait
|
|
350
|
+
const user2 = await fetchUser(2); // Then wait
|
|
351
|
+
return [user1, user2];
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
async function parallel() {
|
|
355
|
+
const [user1, user2] = await Promise.all([fetchUser(1), fetchUser(2)]);
|
|
356
|
+
return [user1, user2];
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
**Advanced Patterns:**
|
|
361
|
+
|
|
362
|
+
```javascript
|
|
363
|
+
// Async IIFE
|
|
364
|
+
(async () => {
|
|
365
|
+
const result = await someAsyncOperation();
|
|
366
|
+
console.log(result);
|
|
367
|
+
})();
|
|
368
|
+
|
|
369
|
+
// Async iteration
|
|
370
|
+
async function processUsers(userIds) {
|
|
371
|
+
for (const id of userIds) {
|
|
372
|
+
const user = await fetchUser(id);
|
|
373
|
+
await processUser(user);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Top-level await (ES2022)
|
|
378
|
+
const config = await fetch("/config.json").then((r) => r.json());
|
|
379
|
+
|
|
380
|
+
// Retry logic
|
|
381
|
+
async function fetchWithRetry(url, retries = 3) {
|
|
382
|
+
for (let i = 0; i < retries; i++) {
|
|
383
|
+
try {
|
|
384
|
+
return await fetch(url);
|
|
385
|
+
} catch (error) {
|
|
386
|
+
if (i === retries - 1) throw error;
|
|
387
|
+
await new Promise((resolve) => setTimeout(resolve, 1000 * (i + 1)));
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Timeout wrapper
|
|
393
|
+
async function withTimeout(promise, ms) {
|
|
394
|
+
const timeout = new Promise((_, reject) =>
|
|
395
|
+
setTimeout(() => reject(new Error("Timeout")), ms),
|
|
396
|
+
);
|
|
397
|
+
return Promise.race([promise, timeout]);
|
|
398
|
+
}
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
## Functional Programming Patterns
|
|
402
|
+
|
|
403
|
+
Functional programming in JavaScript centers on pure functions, immutability, and composable transformations.
|
|
404
|
+
|
|
405
|
+
Key topics covered in [references/advanced-patterns.md](references/advanced-patterns.md):
|
|
406
|
+
- **Array methods** — `map`, `filter`, `reduce`, `find`, `findIndex`, `some`, `every`, `flatMap`, `Array.from`
|
|
407
|
+
- **Higher-order functions** — custom `forEach`/`map`/`filter`, currying, partial application, memoization
|
|
408
|
+
- **Composition and piping** — `compose`/`pipe` utilities with practical data transformation examples
|
|
409
|
+
- **Pure functions and immutability** — immutable array/object operations, deep cloning with `structuredClone`
|
|
410
|
+
|
|
411
|
+
## Modern Class Features
|
|
412
|
+
|
|
413
|
+
ES2022 classes support private fields (`#field`), static fields, getters/setters, and private methods. See [references/advanced-patterns.md](references/advanced-patterns.md) for a full example with inheritance.
|
|
414
|
+
|
|
415
|
+
## Modules (ES6)
|
|
416
|
+
|
|
417
|
+
```javascript
|
|
418
|
+
// Named exports
|
|
419
|
+
export const PI = 3.14159;
|
|
420
|
+
export function add(a, b) { return a + b; }
|
|
421
|
+
|
|
422
|
+
// Default export
|
|
423
|
+
export default function multiply(a, b) { return a * b; }
|
|
424
|
+
|
|
425
|
+
// Import
|
|
426
|
+
import multiply, { PI, add } from "./math.js";
|
|
427
|
+
|
|
428
|
+
// Dynamic import (code splitting)
|
|
429
|
+
const { add } = await import("./math.js");
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
For re-exports, namespace imports, and conditional dynamic loading see [references/advanced-patterns.md](references/advanced-patterns.md).
|
|
433
|
+
|
|
434
|
+
## Iterators and Generators
|
|
435
|
+
|
|
436
|
+
Generators (`function*`) and async generators (`async function*`) enable lazy sequences and async pagination. See [references/advanced-patterns.md](references/advanced-patterns.md) for custom iterator, range generator, fibonacci, and `for await...of` examples.
|
|
437
|
+
|
|
438
|
+
## Modern Operators
|
|
439
|
+
|
|
440
|
+
```javascript
|
|
441
|
+
// Optional chaining — safe property access
|
|
442
|
+
const city = user?.address?.city;
|
|
443
|
+
const result = obj.method?.();
|
|
444
|
+
|
|
445
|
+
// Nullish coalescing — default only for null/undefined (not 0 or "")
|
|
446
|
+
const value = null ?? "default"; // 'default'
|
|
447
|
+
const zero = 0 ?? "default"; // 0
|
|
448
|
+
|
|
449
|
+
// Logical assignment
|
|
450
|
+
a ??= "default"; // assign if null/undefined
|
|
451
|
+
obj.count ||= 1; // assign if falsy
|
|
452
|
+
obj.count &&= 2; // assign if truthy
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
## Performance Optimization
|
|
456
|
+
|
|
457
|
+
See [references/advanced-patterns.md](references/advanced-patterns.md) for debounce, throttle, and lazy evaluation with generators.
|