mdn / sprints

Archived: MDN Web Docs issues are tracked in the content repository.
https://github.com/mdn/content
Creative Commons Zero v1.0 Universal
151 stars 144 forks source link

Micro/Macro task execution order is wrong #4009

Closed CaryWill closed 2 years ago

CaryWill commented 2 years ago

Request type

Details

image

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,

Fetching data
Data fetched
Loaded data

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,

Fetching data
Data fetched
Loaded data

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

CaryWill commented 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().