plugins/aai-stack-node/skills/node-async-patterns/SKILL.md
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/the-answerai/alphaagent-team/blob/main/plugins/aai-stack-node/skills/node-async-patterns/SKILL.md -a claude-code --skill node-async-patternsInstallation paths:
.claude/skills/node-async-patterns/# Node.js Async Patterns Skill
Patterns for asynchronous programming in Node.js.
## Promise Patterns
### Creating Promises
```typescript
// Basic promise
function delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
// Promise with value
function fetchData(): Promise<Data> {
return new Promise((resolve, reject) => {
// async operation
if (success) {
resolve(data)
} else {
reject(new Error('Failed'))
}
})
}
// Promisifying callbacks
import { promisify } from 'util'
import { readFile } from 'fs'
const readFileAsync = promisify(readFile)
const content = await readFileAsync('file.txt', 'utf8')
```
### Promise Combinators
```typescript
// All (parallel, all must succeed)
const [users, posts] = await Promise.all([
fetchUsers(),
fetchPosts(),
])
// All with error handling
const results = await Promise.allSettled([
fetchUsers(),
fetchPosts(),
fetchComments(),
])
results.forEach((result, i) => {
if (result.status === 'fulfilled') {
console.log(`Result ${i}:`, result.value)
} else {
console.error(`Error ${i}:`, result.reason)
}
})
// Race (first to complete)
const result = await Promise.race([
fetchFromServer1(),
fetchFromServer2(),
])
// Any (first to succeed)
const result = await Promise.any([
fetchFromServer1(),
fetchFromServer2(),
])
```
### Promise Utilities
```typescript
// Timeout wrapper
function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
const timeout = new Promise<never>((_, reject) =>
setTimeout(() => reject(new Error('Timeout')), ms)
)
return Promise.race([promise, timeout])
}
// Retry wrapper
async function retry<T>(
fn: () => Promise<T>,
retries: number = 3,
delay: number = 1000
): Promise<T> {
for (let i = 0; i < retries; i++) {
try {
return await fn()
} catch (err) {
if (i === retries - 1) throw err
await new Promise(r => setTimeout(r, delay * Math.pow(2, i)))
}
}
throw n