async & await in JavaScript
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.
async returns promises
async function f() { return 1; } yields Promise<1> — callers await or .then it like any promise.
await pauses locally
let v = await p; suspends this function until p settles — the event loop serves everything else meanwhile.
Rejections throw catchably
try { await risky(); } catch (e) {...} — async errors handle with ordinary synchronous syntax.
Sequential vs parallel
await in loops serializes; Promise.all([...]) parallelizes — choose per dependency, deliberately.
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();Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.