|
| 1 | +## Event Loop |
| 2 | +_Split class in groups of two or three_ |
| 3 | + |
| 4 | +1) How many threads does the browser's Javascript runtime have? |
| 5 | + - *Bonus*: What is a thread? |
| 6 | + |
| 7 | +2) In what order will the colors output to the console? |
| 8 | +``` |
| 9 | +const foo = () => { |
| 10 | + console.log('pink'); |
| 11 | +} |
| 12 | +const bar = () => { |
| 13 | + console.log('black'); |
| 14 | + foo(); |
| 15 | + console.log('blue'); |
| 16 | +} |
| 17 | +bar(); |
| 18 | +``` |
| 19 | + |
| 20 | +3) What is "the stack"? |
| 21 | + |
| 22 | +4) For each line of code below, how many frames are on the call stack? |
| 23 | +``` |
| 24 | +const getDate = () => new Date().toDateString() |
| 25 | +
|
| 26 | +const greet = (name, greeting) => { |
| 27 | + return `${greeting}, ${name}. You arrived on ${getDate()}.` |
| 28 | +} |
| 29 | +
|
| 30 | +greet('mike', 'hello') |
| 31 | +``` |
| 32 | + |
| 33 | +5) What is it called when the javascript engine (node or browser) exceeds the amount of frames it can handle on the stack? |
| 34 | + |
| 35 | +6) What is the order of the colors output to the console? |
| 36 | +``` |
| 37 | +const foo = () => { |
| 38 | + console.log('pink'); |
| 39 | +} |
| 40 | +const bar = () => { |
| 41 | + console.log('black'); |
| 42 | + setTimeout(foo, 0); |
| 43 | + console.log('blue'); |
| 44 | +} |
| 45 | +bar(); |
| 46 | +``` |
| 47 | + |
| 48 | +7) What keeps track of these asynchronous webAPI's? |
| 49 | + - setTimeout |
| 50 | + - addEventListener() |
| 51 | + - fetch() |
| 52 | + |
| 53 | + a) call stack |
| 54 | + b) callback que |
| 55 | + c) event loop |
| 56 | + d) browser |
| 57 | + e) Javascript runtime |
| 58 | + |
| 59 | +8) What is the name of the type of function (called someFunc in the example) that gets called after an asynchronous event? |
| 60 | +``` |
| 61 | +document.querySelector('button').addEventListener('click', someFunc); |
| 62 | +``` |
| 63 | + |
| 64 | +9) A function you've never seen before... What would you guess this function does? Is it synchronous or asynchronous? |
| 65 | +``` |
| 66 | +request('http://www.pokemonapi.dev/info/squirtle', function(error, response, body) { |
| 67 | + console.log(body); |
| 68 | + console.log('Done!'); |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +10) In Javascript land, what does it mean for code to be "blocking"? |
| 73 | + |
| 74 | +11) Which function, when executed, will not block the UI? |
| 75 | +``` |
| 76 | +const countToOneBillionv1 = () => { |
| 77 | + for (let i = 0; i < 10; i++) { |
| 78 | + longRunningOperation(i) |
| 79 | + } |
| 80 | +} |
| 81 | +
|
| 82 | +const countToOneBillionv2 = () => { |
| 83 | + for (let i = 0; i < 10; i++) { |
| 84 | + setTimeout(() => longRunningOperation(i), 0) |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +12) What is the order of the colors output to the console? |
| 90 | +``` |
| 91 | +fetch('http://pokemon.dev/api/id/squirtle') |
| 92 | + .then(result => { |
| 93 | + console.log('blue') |
| 94 | + return fetch('http://pokemon.dev/api/id/charmander') |
| 95 | + }) |
| 96 | + .then(result => { |
| 97 | + console.log('red') |
| 98 | + }) |
| 99 | +
|
| 100 | +console.log('green') |
| 101 | +``` |
| 102 | + |
| 103 | +13) What is the order of the colors output to the console? |
| 104 | +``` |
| 105 | +const foo = async () => { |
| 106 | + console.log('green') |
| 107 | + const result = await fetch('http://pokemon.dev/api/id/squirtle') |
| 108 | + console.log('blue') |
| 109 | + const result = await fetch('http://pokemon.dev/api/id/charmander') |
| 110 | + console.log('red') |
| 111 | +} |
| 112 | +foo(); |
| 113 | +``` |
| 114 | + |
| 115 | +14) What is the order of the colors output to the console? |
| 116 | +``` |
| 117 | +const myArray = ['red', 'blue', 'green']; |
| 118 | +myArray.forEach(function(item) { |
| 119 | + console.log(item); |
| 120 | +}); |
| 121 | +
|
| 122 | +setTimeout(function() { |
| 123 | + console.log('orange'); |
| 124 | +}, 50); |
| 125 | +
|
| 126 | +for (let i=0; i < myArray.length; i++) { |
| 127 | + console.log(myArray[i]); |
| 128 | + if (i === (myArray.length - 1)) { |
| 129 | + setTimeout(function() { |
| 130 | + console.log('black'); |
| 131 | + }, 0); |
| 132 | + } |
| 133 | +} |
| 134 | +
|
| 135 | +``` |
0 commit comments