JavaScript Async/Await Cheat Sheet
Covers async function syntax, error handling with try/catch, and running promises sequentially versus concurrently with Promise combinators.
1 PageIntermediateApr 8, 2026
Basic async/await
async functions always return a Promise.
javascript
async function fetchUser(id) { const response = await fetch(`/api/users/${id}`); const data = await response.json(); return data;}// An async function always returns a PromisefetchUser(1).then(user => console.log(user));// Equivalent, inside another async functionasync function main() { const user = await fetchUser(1); console.log(user);}
Error Handling
Wrap awaited calls in try/catch/finally.
javascript
async function loadUser(id) { try { const res = await fetch(`/api/users/${id}`); if (!res.ok) { throw new Error(`HTTP ${res.status}`); } return await res.json(); } catch (err) { console.error("Failed to load user:", err.message); return null; } finally { console.log("request finished"); }}
Sequential vs Parallel
Awaiting in sequence pays the latency cost multiple times.
javascript
// Sequential -- each await blocks the next (slower, ~2x time)async function sequential() { const a = await fetchUser(1); const b = await fetchUser(2); return [a, b];}// Parallel -- both requests start immediatelyasync function parallel() { const [a, b] = await Promise.all([fetchUser(1), fetchUser(2)]); return [a, b];}// Parallel, tolerant of individual failuresasync function parallelSettled() { const results = await Promise.allSettled([fetchUser(1), fetchUser(2)]); return results.filter(r => r.status === "fulfilled").map(r => r.value);}
Iteration & Top-Level Await
Looping with await and module-level await.
javascript
async function processAll(ids) { const results = []; for (const id of ids) { results.push(await fetchUser(id)); // Runs one at a time } return results;}// Top-level await (in ES modules only)const config = await fetch("/config.json").then(r => r.json());
Promise Combinators
Ways to combine multiple promises.
- Promise.all()- Waits for all promises; rejects immediately if any one rejects
- Promise.allSettled()- Waits for all promises; never short-circuits, returns a status per item
- Promise.race()- Resolves/rejects as soon as the first promise settles
- Promise.any()- Resolves with the first fulfilled promise; rejects only if all reject
- await- Pauses the async function until the promise settles, unwrapping its value
- async function- Always returns a Promise, even if the body has no explicit await
Pro Tip
Awaiting independent promises one-by-one inside a loop serializes requests unnecessarily — start them all first (e.g. with map + Promise.all) so they run concurrently instead of paying the latency cost N times.
Was this cheat sheet helpful?
Explore Topics
#JavaScriptAsyncAwait#JavaScriptAsyncAwaitCheatSheet#Programming#Intermediate#BasicAsyncAwait#ErrorHandling#SequentialVsParallel#Iteration#Functions#Concurrency#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance