Created
June 28, 2017 11:08
-
-
Save OlehRovenskyi/e3613f46b87bb05f3bbe4ffb9711dc30 to your computer and use it in GitHub Desktop.
event loop examples
This file contains 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
function multiply(a, b) { | |
return a * b; | |
}; | |
function square(n) { | |
return multiply(n, n) | |
}; | |
function printSquare(n) { | |
var squared = square(n); | |
console.log(squared); | |
}; | |
printSquare(4); | |
// ------- call stack ------- | |
// multiply(n, n) | |
// square(n) | |
// printSquare(4) // when clean -> add console.log(squared); -> clean | |
// main() | |
// -------------------------- | |
function foo(a, b) { | |
throw new Error('Oops!'); | |
}; | |
function bar(n) { | |
foo(); | |
}; | |
function baz(n) { | |
bar(); | |
}; | |
baz(); | |
// we see stack trace in console when error happen | |
function foo() { | |
return foo(); | |
} | |
foo(); | |
// recursiv = Maximum call stack size exceeded | |
// main() -> foo() -> foo() ... | |
// ------------------------- | |
console.log('Hi'); | |
setTimeout(function() { | |
console.log('There'); | |
}, 5000); | |
console.log('All'); | |
// Hi -> All -> There | |
console.log('Hi'); | |
setTimeout(function() { | |
console.log('There'); | |
}, 0); | |
console.log('All'); | |
// Hi -> All -> There | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment