Event, CustomEvent, EventTarget, AbortController and AbortSignal are globals and follow the
DOM Standard. Fetch, timers, streams and addEventListener accept an abort signal.
Events
js
// events.mjs
const bus = new EventTarget();
bus.addEventListener("order", (event) => console.log("order", event.detail.id), { once: true });
bus.addEventListener("order", (event) => {
event.preventDefault();
console.log("cancelable", event.cancelable);
});
const event = new CustomEvent("order", { detail: { id: 7 }, cancelable: true });
console.log("dispatched", bus.dispatchEvent(event), event.defaultPrevented);
bus.dispatchEvent(new CustomEvent("order", { detail: { id: 8 } }));
utf-8 · js · Lines: 11
elide run events.mjs prints:
text
order 7
cancelable true
dispatched false true
cancelable false
utf-8 · text · Lines: 4
Differences from browsers
- There is no DOM tree: dispatch has one target and no capture or bubble phase.
- During dispatch,
eventPhaseisEvent.AT_TARGETandcomposedPath()holds only the current target. - A listener that throws is reported; the remaining listeners still run.
Abort signals
AbortSignal.abort(), AbortSignal.any(), AbortSignal.timeout() and the signal option of addEventListener
work as in browsers.
js
// abort.mjs
const controller = new AbortController();
controller.signal.addEventListener("abort", () => console.log("aborted:", controller.signal.reason));
controller.abort("user cancelled");
const timeout = AbortSignal.timeout(10);
await new Promise((resolve) => timeout.addEventListener("abort", resolve));
console.log(timeout.reason.name);
const either = AbortSignal.any([new AbortController().signal, AbortSignal.abort("first")]);
console.log(either.aborted, either.reason);
utf-8 · js · Lines: 11
elide run abort.mjs prints:
text
aborted: user cancelled
TimeoutError
true first
utf-8 · text · Lines: 3
Reference
Status: Full · Not measured · since 1.1.0
Exports
| Export | Kind | Status | Notes |
|---|---|---|---|
Event | class | Full | type, target, currentTarget, NONE, CAPTURING_PHASE, AT_TARGET, BUBBLING_PHASE, eventPhase, composedPath(), stopPropagation(), stopImmediatePropagation(), bubbles, cancelable, preventDefault(), defaultPrevented, composed, isTrusted, timeStamp, cancelBubble |
CustomEvent | class | Full | detail |
EventTarget | class | Full | addEventListener(), removeEventListener(), dispatchEvent() |
AbortController | class | Full | signal, abort() |
AbortSignal | class | Full | AbortSignal.abort(), AbortSignal.timeout(), AbortSignal.any(), aborted, reason, throwIfAborted(), onabort |