Last active
July 29, 2023 18:34
-
-
Save Jeneko/a98f858e129c7854eb6660389e1003ca to your computer and use it in GitHub Desktop.
Where event listeners go - in macro- or in microtask queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.resolve('1').then(console.log); | |
document.addEventListener('testEvent', () => console.log('2')); | |
document.dispatchEvent(new Event('testEvent')); | |
console.log('3') | |
Promise.resolve('4').then(console.log); | |
// Related question at StackOverflow | |
// https://stackoverflow.com/questions/71324258/the-callback-function-of-addeventlistener-is-queued-in-macro-task-queue-in-event | |
// EXPLANATION: dispatchEvent invokes event handlers SYNCHRONOUSLY! | |
// ADDITION: element.click() also invokes handlers SYNCHRONOUSLY! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment