Closed CaryWill closed 2 years ago
Unlike "native" events, which are fired by the DOM and invoke event handlers asynchronously via the event loop, dispatchEvent() invokes event handlers synchronously. All applicable event handlers will execute and return before the code continues on after the call to dispatchEvent().
Request type
Details
Page URL
It should produce the same result.
The first time
getData
function get's called, control flow comes to else clause, which enqueues a microtask, then inside the microtask, it triggers a load event which in turn enqueues a task to the event loop, so, it should print out,The second time
getData
function get's called, control flow comes to if...else clause, which just enqueues a task to the event loop, so, it should also print out,No matter what type of task(micro/macro) get's enqueued, the stack should be emptied first, which, the two
console.log
should get's called first.Created a Demo