Skip to content

Instantly share code, notes, and snippets.

@OlehRovenskyi
Created June 28, 2017 11:08
Show Gist options
  • Save OlehRovenskyi/e3613f46b87bb05f3bbe4ffb9711dc30 to your computer and use it in GitHub Desktop.
Save OlehRovenskyi/e3613f46b87bb05f3bbe4ffb9711dc30 to your computer and use it in GitHub Desktop.
event loop examples
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