Skip to content

Instantly share code, notes, and snippets.

@Goloburda
Last active July 11, 2021 15:45
Show Gist options
  • Save Goloburda/b6e2ec38052e417605a2acc08ad78c7c to your computer and use it in GitHub Desktop.
Save Goloburda/b6e2ec38052e417605a2acc08ad78c7c to your computer and use it in GitHub Desktop.
Micro/Macro tasks
// https://www.youtube.com/watch?v=cCOL7MC4Pl0
const button = document.getElementsByTagName('button')[0]
button.addEventListener('click', () => {
setTimeout(() => console.log('Timeout 1'))
Promise.resolve().then(() => console.log('Microtask - 1'))
console.log('task 1')
})
button.addEventListener('click', () => {
setTimeout(() => console.log('Timeout 2'))
Promise.resolve().then(() => console.log('Microtask - 2'))
console.log('task 2')
})
/*
"task 1"
"Microtask - 1"
"task 2"
"Microtask - 2"
"Timeout 1"
"Timeout 2"
*/
// button.click()
/*
"task 1"
"task 2"
"Microtask - 1"
"Microtask - 2"
"Timeout 1"
"Timeout 2"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment