Skip to content

Instantly share code, notes, and snippets.

@Jeneko
Last active July 29, 2023 18:34
Show Gist options
  • Save Jeneko/a98f858e129c7854eb6660389e1003ca to your computer and use it in GitHub Desktop.
Save Jeneko/a98f858e129c7854eb6660389e1003ca to your computer and use it in GitHub Desktop.
Where event listeners go - in macro- or in microtask queue
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