Master asynchronous programming in Node.js with Promises, async/await, streams, and event-driven patterns for efficient non-blocking operations
Handles Node.js async operations with Promises, async/await, and streams. Use when writing non-blocking I/O code, managing concurrent requests, or processing data streams efficiently.
/plugin marketplace add pluginagentmarketplace/custom-plugin-nodejs/plugin install nodejs-developer-plugin@pluginagentmarketplace-nodejsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
assets/config.yamlreferences/GUIDE.mdscripts/helper.pyMaster asynchronous programming - the foundation of Node.js performance and scalability.
Three pillars of async JavaScript:
// 1. Callbacks (old way)
fs.readFile('file.txt', (err, data) => {
if (err) throw err;
console.log(data);
});
// 2. Promises (better)
fs.promises.readFile('file.txt')
.then(data => console.log(data))
.catch(err => console.error(err));
// 3. Async/Await (modern)
async function readFile() {
try {
const data = await fs.promises.readFile('file.txt');
console.log(data);
} catch (err) {
console.error(err);
}
}
// ❌ Slow: Sequential (300ms total)
async function slow() {
const a = await fetch('/api/a'); // 100ms
const b = await fetch('/api/b'); // 100ms
const c = await fetch('/api/c'); // 100ms
return [a, b, c];
}
// ✅ Fast: Parallel (100ms total)
async function fast() {
const [a, b, c] = await Promise.all([
fetch('/api/a'),
fetch('/api/b'),
fetch('/api/c')
]);
return [a, b, c];
}
// Promise.all - Wait for all (fails if one fails)
const [users, posts] = await Promise.all([getUsers(), getPosts()]);
// Promise.allSettled - Wait for all (never fails)
const results = await Promise.allSettled([fetch1(), fetch2(), fetch3()]);
// Promise.race - First to complete
const fastest = await Promise.race([server1(), server2()]);
// Promise.any - First to succeed
const result = await Promise.any([tryAPI1(), tryAPI2()]);
// Try/catch for async/await
async function safeOperation() {
try {
const result = await riskyOperation();
return { success: true, data: result };
} catch (error) {
console.error('Operation failed:', error);
return { success: false, error: error.message };
}
}
// Process-level handlers
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection:', reason);
});
async function retryWithBackoff(fn, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
if (i === maxRetries - 1) throw error;
const delay = Math.pow(2, i) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
}
}
}
async function batchProcess(items, concurrency = 5) {
const results = [];
for (let i = 0; i < items.length; i += concurrency) {
const batch = items.slice(i, i + concurrency);
const batchResults = await Promise.all(
batch.map(item => processItem(item))
);
results.push(...batchResults);
}
return results;
}
const { pipeline } = require('stream');
const fs = require('fs');
// Efficient file processing
pipeline(
fs.createReadStream('input.txt'),
transformStream,
fs.createWriteStream('output.txt'),
(err) => {
if (err) console.error('Pipeline failed:', err);
else console.log('Pipeline succeeded');
}
);
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const emitter = new MyEmitter();
emitter.on('event', (data) => {
console.log('Event fired:', data);
});
emitter.emit('event', { id: 123 });
Use async patterns when:
This skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
This skill should be used when the user asks to "create an agent", "add an agent", "write a subagent", "agent frontmatter", "when to use description", "agent examples", "agent tools", "agent colors", "autonomous agent", or needs guidance on agent structure, system prompts, triggering conditions, or agent development best practices for Claude Code plugins.
This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.