Async JavaScript
Promises, async/await, error handling, parallel execution, and common async patterns.
Promises
1 topic
Creating Promises
The foundation of async JavaScript
// Create a promise
const promise = new Promise((resolve, reject) => {
const success = true;
if (success) resolve("Done!");
else reject(new Error("Failed"));
});
// Chain promises
promise
.then(result => console.log(result))
.catch(error => console.error(error))
.finally(() => console.log("Cleanup"));💡 Always handle rejections with .catch()
📌 finally() runs regardless of outcome
Async/Await
1 topic
Basic Usage
Modern syntax for working with promises
// Async function
async function fetchUser(id) {
try {
const response = await fetch(`/api/users/${id}`);
if (!response.ok) throw new Error("Not found");
return await response.json();
} catch (error) {
console.error("Fetch failed:", error);
throw error;
}
}
// Arrow function
const getUser = async (id) => {
const res = await fetch(`/api/users/${id}`);
return res.json();
};💡 async functions always return a Promise
⚡ await pauses only the current function, not the thread
Parallel Execution
1 topic
Promise Combinators
Run multiple async operations concurrently
// Promise.all - wait for ALL (fails fast)
const [users, posts] = await Promise.all([
fetchUsers(),
fetchPosts()
]);
// Promise.allSettled - wait for ALL regardless
const results = await Promise.allSettled([
fetchA(), fetchB(), fetchC()
]);
// Promise.race - first to settle
const fastest = await Promise.race([
fetchFromCDN1(),
fetchFromCDN2()
]);
// Promise.any - first to succeed
const first = await Promise.any([
tryServer1(),
tryServer2()
]);💡 Use Promise.all for independent parallel tasks
📌 allSettled never rejects - check each result status
Common Patterns
1 topic
Retry & Timeout
Resilient async patterns for production code
// Retry with delay
async function retry(fn, retries = 3, delay = 1000) {
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));
}
}
}
// Timeout wrapper
function withTimeout(promise, ms) {
const timeout = new Promise((_, reject) =>
setTimeout(() => reject(new Error("Timeout")), ms)
);
return Promise.race([promise, timeout]);
}
// Usage
const data = await retry(() => withTimeout(fetchData(), 5000));💡 Always set timeouts for network requests
⚡ Exponential backoff: delay * 2^attempt
Related Cheat Sheets
More hands-on references connected to this topic.
Modern JavaScript — ES6+, arrays, objects, promises, destructuring, and common patterns.
shared topics
FastAPI routes, request models, dependency injection, authentication, and async patterns.
shared topics
Complete guide to building AI agents — architecture, tool use, memory, planning patterns, multi-agent systems, frameworks, evaluation, and production best practices.
same difficulty, keyword overlap
Regular expression syntax, character classes, groups, lookaheads, and common patterns.
shared topics
Related Articles
Background reading and deeper explanations for this sheet.
FastAPI Interview Questions: Practical Cheat Sheet for Developers
Preparing for FastAPI interviews can feel overwhelming if you only memorize definitions. This practical cheat sheet helps you answer common FastAPI interview questions with confidence using real-world examples, production-ready patterns, and beginner-friendly explanations with depth.
keyword overlap
Evaluation of LLM Systems: 12 Overlooked Metrics That Actually Predict Production Success
Most LLM teams track accuracy and latency, then wonder why users still churn. This practical guide covers underused evaluation metrics that reveal real-world quality, safety, cost, and reliability issues before they hit production. You’ll get concrete formulas, examples, and implementation patterns you can apply immediately.
keyword overlap
Handling AI Hallucinations in Real Applications: A Practical Guide for Developers and Product Teams
Hallucinations are one of the biggest blockers to shipping trustworthy AI features. In this practical guide, you’ll learn how to detect, reduce, and contain hallucinations in production with proven patterns, real-world examples, and implementation-ready code.
keyword overlap
System Design for 100 Million Requests/Day: Scalable Patterns Made Simple
Designing for 100 million requests a day sounds intimidating, but it becomes manageable when you break traffic into layers and apply proven patterns. In this guide, you’ll learn the architecture, capacity math, and practical system design patterns—like caching, queue-based load leveling, and CQRS—with easy examples you can reuse.
keyword overlap