Events & cancellation
The WHATWG event and cancellation trio — EventTarget, Event/CustomEvent, and AbortController/AbortSignal — kept distinct from Node's EventEmitter, and implemented as Node implements them: in TypeScript, subclassable, reached as globals. Dispatch is single-target as in Node (there is no DOM tree). An AbortSignal cancels a fetch — including via AbortSignal.timeout(ms) — but is not yet wired into setTimeout.
EventTarget / addEventListener / dispatchEvent
ExactNode's EventTarget (written in TypeScript, like Node's own JavaScript): addEventListener with once/passive/capture/signal and handleEvent objects, duplicate suppression, removeEventListener, dispatchEvent returning !defaultPrevented. A listener's throw is reported as an uncaught exception on the next tick while dispatch continues. Subclassable: class X extends EventTarget.
Parameters- typestring
- Event type name to register or fire.
- listener(e: Event) => void
- A function (called with this = the target) or an object with handleEvent.
- opts{ once?, capture?, passive?, signal? }
- once, passive (preventDefault is ignored), capture (part of the listener's identity), signal (aborting removes the listener).
boolean — dispatchEvent returns false when a listener called preventDefault().
const bus = new EventTarget()
bus.addEventListener("ping", (e: Event) => console.log("ping"))
bus.dispatchEvent(new Event("ping"))Event / CustomEvent
ExactEvent and CustomEvent with the full property set: type, bubbles, cancelable, composed, defaultPrevented, timeStamp, isTrusted, target/currentTarget, eventPhase, returnValue, cancelBubble, composedPath(), preventDefault(), stopPropagation(), stopImmediatePropagation(); CustomEvent.detail (null by default). Both subclassable.
Parameters- typestring
- The event type name.
- init{ detail?, cancelable?, bubbles? }
- CustomEvent.detail and cancelable are read; bubbles is accepted but ignored.
Event | CustomEvent — The constructed event object.
const clicked = new Event("click", { cancelable: true })
clicked.preventDefault()
console.log(clicked.defaultPrevented) // true
const greeting = new CustomEvent("greet", { detail: "hello" })
console.log(greeting.detail) // helloAbortController / AbortSignal
ExactThe WHATWG cancellation token. An AbortSignal is an EventTarget that fires an "abort" event, plus an aborted flag and a reason. Listen with signal.addEventListener("abort", cb) or the signal.onabort = cb event-handler property (fired together on controller.abort(); = null clears it). fetch(url, { signal }) cancellation works — pre-flight, mid-flight, and via AbortSignal.timeout(ms) — on both the top-level await path and the fiber path inside an http.listen handler: the transfer is torn down and the fetch rejects with signal.reason itself, Node parity — by default a DOMException named AbortError for a manual abort and TimeoutError for a timeout, and a custom abort(reason) value rethrown as-is. DOMException inherits Error, so both instanceof DOMException and instanceof Error hold; new DOMException(message?, name?) is constructible directly.
Parameters- reasonany
- Optional abort reason stored on signal.reason — any value, type preserved; defaults to an AbortError DOMException. A never-aborted signal's reason is undefined.
- msnumber
- AbortSignal.timeout(ms) deadline in milliseconds.
AbortController | AbortSignal — The controller, or a signal from AbortSignal.timeout.
const controller = new AbortController()
controller.abort()
try {
await fetch("http://example.com", { signal: controller.signal })
} catch (e) {
console.log(e.name) // AbortError
}