dispatchEvent() is synchronous. It builds the path, calls every listener in order, runs any activation behaviour, and only then returns true, or false if the event was canceled. Code after the call sees every listener's side effects. A listener that throws is reported to window's error event; the caller's code carries on.
Microtasks (promise reactions, queueMicrotask()) run at a microtask checkpoint. The HTML Standard performs one in clean up after running script: whenever a callback returns and the JavaScript execution context stack is now empty. Web IDL runs that cleanup after every listener call.
So the difference is who is on the stack:
- A real click. The browser dispatches the event from a task with no script running. Each listener returns to an empty stack, so its microtasks run before the next listener starts, and before the default action.
dispatchEvent()orel.click()from script. Your calling code is still on the stack, so the stack never empties between listeners. All the microtasks wait until the outermost script returns, after the whole dispatch.
This is about the stack, not about isTrusted. A trusted focus event fired synchronously by el.focus() behaves like the scripted case.
It matters when a listener awaits and then touches the event. After an await in a real-click listener, dispatch is still in progress: currentTarget is set and preventDefault() still cancels the default action. With el.click(), the same code runs after dispatch ended: currentTarget is null and canceling is too late. Tests that use el.click() or dispatchEvent() can therefore pass while real users see different behaviour.