Async cancellation

비동기 취소

Propagate cancellation to unneeded work and release its resources.

···
html
<div class="v"><header><b>CANCEL → CLEANUP</b><span id="state"></span></header><main><div class="steps" id="steps"></div><div class="notice" id="notice"></div></main><footer>signal, observe, clean up</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} .steps{height:62%;display:flex;align-items:center;justify-content:space-between;gap:3px}.steps b{flex:1;padding:12px 2px;border:1px solid var(--line);border-radius:6px;text-align:center;font:600 .82em ui-monospace,monospace}.notice{text-align:center;color:var(--accent);font:600 .85em ui-monospace,monospace}
js
let n=0;const labels=["running","signal","cleanup","stopped"];function draw(){document.getElementById("steps").innerHTML=labels.map((x,i)=>"<b class=\""+(i===n?"active":i>n?"muted":"")+"\">"+x+"</b>").join("");document.getElementById("notice").textContent=n===2?"release resources before exit":n===3?"no child left running":"parent controls lifetime";document.getElementById("state").textContent=labels[n];n=(n+1)%4}draw();setInterval(draw,950)

An async result may no longer matter after navigation or a parent failure. Propagating a cancellation signal gives child work a chance to stop.

Cancellation is not always an immediate forced exit. Work should observe the signal at safe points, clean up files, locks, or network resources, and then finish.

When to use

Use it for user-abortable requests and child work bound to a parent lifetime.

Open as page ↗