← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

async / await in JavaScript

javascript ES2017 Intermediate

Also Known As

async functions await keyword ES2017 async async/await pattern

TL;DR

async functions always return a Promise; await pauses execution inside an async function until a Promise settles — giving asynchronous code the readability of synchronous code without blocking the event loop.

Explanation

async/await is syntactic sugar over Promises, introduced in ES2017. Marking a function async makes it return a Promise implicitly — even a bare return value is wrapped. Inside an async function, await suspends execution until the awaited Promise resolves, then resumes with the resolved value. If the Promise rejects, await throws the rejection reason, making try/catch the natural error-handling pattern. Crucially, await does not block the thread — while waiting, the event loop is free to run other tasks. Multiple independent async operations should be parallelised with Promise.all() rather than awaited sequentially. Top-level await is supported in ES modules. Common pitfalls: forgetting await (getting a Promise instead of its value), sequential awaiting in a loop instead of parallelising, and unhandled rejections when an async function throws inside an event handler with no wrapping try/catch.

Watch Out

An async function called inside addEventListener or forEach without await propagates an unhandled Promise rejection that is very difficult to debug — always add try/catch inside async event handlers.

Common Misconception

await does not block the thread or pause the event loop — it only suspends the current async function, allowing other callbacks and tasks to run while waiting.

Why It Matters

async/await eliminates callback hell and Promise chain nesting, making asynchronous code readable and maintainable — most modern JS codebases including Node.js backends and browser fetch calls rely on it.

Common Mistakes

  • Forgetting await — const data = fetch(url) gives a Promise object, not the response body.
  • Awaiting in a loop sequentially when the iterations are independent — use Promise.all() to run them in parallel.
  • Missing try/catch around await — an unhandled rejection inside an async function crashes Node.js workers or produces silent failures in browsers.
  • Using async in an Array.forEach callback — forEach does not await the returned Promises, so the loop finishes before the async work completes.

Code Examples

💡 Note
The bad example awaits each fetch in sequence — 10 requests × 200ms = 2 seconds total. Promise.all fires all requests simultaneously, completing in ~200ms regardless of count.
✗ Vulnerable
// Sequential awaits — total time = sum of all request times:
async function loadAll(ids) {
    const results = [];
    for (const id of ids) {
        results.push(await fetch(`/api/${id}`)); // waits for each in turn
    }
    return results;
}
✓ Fixed
// Parallel with Promise.all — total time = slowest single request:
async function loadAll(ids) {
    const promises = ids.map(id => fetch(`/api/${id}`));
    return Promise.all(promises); // all requests in flight simultaneously
}

Added 10 Apr 2026
Views 38
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 2 pings F 1 ping S 1 ping S 1 ping M 0 pings T 0 pings W 1 ping T
No pings yesterday
Google 7 Perplexity 4 ChatGPT 2 SEMrush 2 Meta AI 2 Unknown AI 1 Ahrefs 1 Qwen 1
crawler 19 crawler_json 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Replace sequential await-in-loop with Promise.all(items.map(item => asyncFn(item)))
🔗 Prerequisites
🔍 Detection Hints
await inside for...of or for loop body where iterations are independent
Auto-detectable: ✓ Yes eslint
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Low Context: Function Tests: Update

✓ schema.org compliant