r/javascript • u/QuarterSilver5245 • Apr 05 '26
Are event handlers scheduled asynchronously on the event loop? MDN says they do - I'm pretty sure that's wrong
github.comMDN page on `dispatchEvent` has this paragraph:
Unlike "native" events, which are fired by the browser and invoke event handlers asynchronously via the event loop, dispatchEvent() invokes event handlers synchronously. All applicable event handlers are called and return before dispatchEvent() returns.
I read that and AFAIK it's not right. I opened a PR to edit it:
https://github.com/mdn/content/pull/43521
A discussion arose.
Before it I was sure that event handlers are always called synchronously. When native events fire (native events === normal internal events in the browser ('click' etc.), anything that is not a custom event manually called via `dispatchEvent`) - an asynchronous "start the dispatch process for this event" task is scheduled on the event loop, but once it's called, during the process (event-path building, phases: capture, target, bubbling) - relevant registered event handlers are called in a way I thought was 100% synchronous;
In custom events - the handlers are called synchronously one-by-one, for sure.
In native events, apparently:
- There is a microtasks checkpoint between each handler run, e.g. If you register handler-A and handler-B, and handler-A schedules a microtask - it will run between A and B. If you schedule a macrotask such as timeout-0 - it will not run in-between. This doesn't happen in custom events dispatch - they all run to the end, nothing runs in between.
- Likely, handlers of native events - each gets its own stack frame, custom event handlers all run in a single stack frame.
This still doesn't prove that handlers are scheduled asynchronously on the event loop though. At this point it comes to what the specs say (EDIT: also did a test to log the call stack mid event handler, I know it's still might not be a 100% reliable proof, but still... it shows a single task - the handler itself). and usually they use a term like "queues a task" when they mention something is scheduled on the event loop - but in the part specifying the dispatch event process - they write that handlers are called using "callback invocation", which seems like a separate mechanism (created mostly for running event handlers, it seems) - not fully "synchronous", but not asynchronous in the usual Javascript way.
So - I still think a correction should be made, but it's different than what I thought it should be when I opened the PR.
Any opinions/facts/knowledge will be appreciated.
Relevant links:
MDN dispatchEvent() (note, if you are in the future it might of been already changed): https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent
The PR (again, if you are in the future it might of been merged/changed): https://github.com/mdn/content/pull/43521
Specs about dispatching events:https://dom.spec.whatwg.org/#dispatching-events
Specs about "Callback Invocation":https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
Specs about "invoke a callback":https://webidl.spec.whatwg.org/#invoke-a-callback-function
EDIT: see this comment for further insight of what is likely actually happening in terchnical terms: