Part 06 · Async · 6.1.06

Tasks, microtasks, Promise chains và cancellation

Async không tự song song CPU; host event loop chọn task, chạy JS đến completion rồi drain microtasks trước rendering opportunities.

Ordering rule: một microtask chain dài có thể starve rendering/tasks; timer delay là minimum delay, không phải deadline thực thi.

1. Event loop và queues

Host có task sources/queues; sau task, microtask checkpoint chạy Promise reactions và queueMicrotask trước cơ hội render/next task. Browser/Node có host-specific phases, nên test observable ordering thay vì dùng một sơ đồ universal.

task -> run JS to completion -> drain microtasks -> render opportunity -> next task

2. Promise semantics

Promise là eventual settlement; then trả promise mới và adopts returned thenable. Error propagate theo chain; thiếu return tạo floating work và thiếu catch có thể thành unhandled rejection. Promise không làm CPU-bound code chạy song song.

CombinatorBehaviorUse
allFail-fast khi một rejectAll results required, side effects đã có cleanup
allSettledThu mọi outcomeBatch/report từng item
raceFirst settledTimeout wrapper nhưng phải abort loser
anyFirst fulfilledFallback replicas; AggregateError nếu tất cả fail

3. Concurrency, stale response và backpressure

Giới hạn concurrency theo resource/dependency thay vì Promise.all hàng nghìn item. Guard stale responses bằng request identity/version khi user đổi query nhanh; queue/buffer phải bounded để producer không làm cạn memory.

4. Cancellation và timeout

Promise không cancel intrinsic. AbortController/AbortSignal truyền cancellation tới fetch/API hỗ trợ; cleanup listener/timer và distinguish abort khỏi genuine error. Timeout nên abort underlying work, không chỉ race một promise rồi để socket/timer tiếp tục.

const controller = new AbortController();
const timer = setTimeout(() => controller.abort("deadline"), 1000);
try { await fetch(url, {signal: controller.signal}); }
finally { clearTimeout(timer); }
Production checklist: có deadline, cancellation propagation, bounded concurrency, cleanup loser, unhandled-rejection telemetry và test ordering dưới load.
Tài liệu: WHATWG Event Loops · MDN Promises · MDN AbortController