Learn JavaScript
❯ async.async_await

async & await in JavaScript

$concept

async functions always return promises; await pauses that function until its operand settles — value passes through, rejection throws catchably. Code reads synchronously (try/catch works! loops work!) while executing asynchronously — the definitive solution to callback pyramids and .then chains.

await suspends only its own async function (callers continue), yields to microtasks (not macrotasks), and in loops runs sequentially (await in for...of) versus Promise.all for parallelism. Top-level await works in modules; classic scripts wrap in async IIFEs.

$how it works
1

async returns promises

async function f() { return 1; } yields Promise<1> — callers await or .then it like any promise.

2

await pauses locally

let v = await p; suspends this function until p settles — the event loop serves everything else meanwhile.

3

Rejections throw catchably

try { await risky(); } catch (e) {...} — async errors handle with ordinary synchronous syntax.

4

Sequential vs parallel

await in loops serializes; Promise.all([...]) parallelizes — choose per dependency, deliberately.

$example
function wait(ms, v) {
return new Promise((r) => setTimeout(() => r(v), ms));
}
async function main() {
let a = await wait(5, "a");
let b = await wait(5, "b");
console.log(a + b);
}
main();
Output
ab
1Each await suspends main until its timer promise settles — sequentially, a then b.
2Resolved values arrive as ordinary variables — no callbacks, no chains.
3ab prints — synchronous-looking code with asynchronous execution.
$use cases
Sequential async workflows (fetch → parse → render) in readable form
try/catch error handling across awaited operations
Parallel fan-out with Promise.all plus awaited join
Top-level await in modules (async IIFE wrapper elsewhere)

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.