Async/await

async/await

Await pauses the rest of an async function and resumes it after settlement.

···
html
<div class="v"><header><b>AWAIT RESUME</b><span id="state"></span></header><main><div class="lanes"><div class="lane" id="fn"></div><div class="lane" id="other"></div></div></main><footer>caller continues while function waits</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,3.2vmin,17px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.28} .lanes{height:100%;display:flex;flex-direction:column;justify-content:center;gap:10px}.lane{height:37%;border:1px solid var(--line);border-radius:6px;padding:6px;display:flex;align-items:center;justify-content:space-around;font:600 .85em ui-monospace,monospace}.lane span{padding:5px;border-radius:4px}.lane .on{background:var(--accent);color:var(--bg)}
js
let n=0;function draw(){const a=["start","await","resume"],b=["caller","other work","done"];document.getElementById("fn").innerHTML=a.map((x,i)=>"<span class=\""+(i===n?"on":"")+"\">"+x+"</span>").join("→");document.getElementById("other").innerHTML=b.map((x,i)=>"<span class=\""+(i===n?"on":"")+"\">"+x+"</span>").join("→");document.getElementById("state").textContent=["invoke","suspended","settled"][n];n=(n+1)%3}draw();setInterval(draw,1000)

At await, the rest of an async function waits for the awaited value to settle. The caller receives a Promise immediately and can continue other work.

Await does not block the whole main thread. Awaiting independent requests one by one can increase total time; start them together when their dependencies allow it.

When to use

Use it to write Promise based workflows in readable sequence.

Open as page ↗