|
| 1 | +# [Episode 11](https://www.youtube.com/watch?v=lW_erSjyMeM&list=PLlasXeu85E9cQ32gLCvAvr9vNaUccPVNP&index=13): setTimeout + Closures Interview Question 🔥 |
| 2 | + |
| 3 | +Let us understand by a simple example. |
| 4 | + |
| 5 | +``` |
| 6 | +function x(){ |
| 7 | + for(var i=0; i<=5; i++){ |
| 8 | + setTimeout(function (){ |
| 9 | + console.log(i); |
| 10 | + }, i*1000); |
| 11 | + } |
| 12 | + console.log("Namaste JS"); |
| 13 | +} |
| 14 | +x(); |
| 15 | +``` |
| 16 | +Expected: |
| 17 | +>Namaste JS |
| 18 | +> 1 (after 1s) |
| 19 | +> 2 (after 2s) |
| 20 | +> 3 (after 3s) |
| 21 | +> 4 (after 4s) |
| 22 | +> 5 (after 5s) |
| 23 | +> 6 (after 6s) |
| 24 | +
|
| 25 | +Output: |
| 26 | +>Namaste JS |
| 27 | +>6 |
| 28 | +>6 |
| 29 | +>6 |
| 30 | +>6 |
| 31 | +>6 |
| 32 | +>6 |
| 33 | +
|
| 34 | +**What happens BTS?** |
| 35 | +When the JS compiler sees the `setTimeout` it puts it in Async Queue. It sees it 5 times, and fills the queue with a total of 5 `setTimeout` funcs. |
| 36 | + |
| 37 | +Since `setTimeout` is async function, it is pushed into queue and the main thread of JS continues and it prints `Namaste JS` first and meanwhile the reference `i = 6` after incrementing from the `for` loop. |
| 38 | + |
| 39 | +After the sync thread is executed and `call stack` is empty, the async queue is started being emptied. |
| 40 | + |
| 41 | +Here, all the `setTimeout`s are closures, and thus they have reference of lexical env; and here in lexical env, `i=6` and thus `6` is printed without delay. |
| 42 | + |
| 43 | +### How to solve this? |
| 44 | + |
| 45 | +``` |
| 46 | +function x(){ |
| 47 | + for(let i=0; i<=5; i++){ |
| 48 | + setTimeout(function (){ |
| 49 | + console.log(i); |
| 50 | + }, i*1000); |
| 51 | + } |
| 52 | + console.log("Namaste JS"); |
| 53 | +} |
| 54 | +x(); |
| 55 | +``` |
| 56 | +Output: |
| 57 | +>Namaste JS |
| 58 | +> 1 (after 1s) |
| 59 | +> 2 (after 2s) |
| 60 | +> 3 (after 3s) |
| 61 | +> 4 (after 4s) |
| 62 | +> 5 (after 5s) |
| 63 | +> 6 (after 6s) |
| 64 | +
|
| 65 | +**Why this works?** |
| 66 | + |
| 67 | +We know that `let` has block scope. And thus for every block, a new value of `i` is created for each clossure. |
| 68 | + |
| 69 | +And so, in async queue, all `setTimeout` has unique value of `i` at a different memory location. |
| 70 | + |
| 71 | +### How to solve this without using `let`? |
| 72 | + |
| 73 | +``` |
| 74 | +function x(){ |
| 75 | + for(var i=0; i<=5; i++){ |
| 76 | + function close(x){ |
| 77 | + setTimeout(function (){ |
| 78 | + console.log(i); |
| 79 | + }, x*1000); |
| 80 | + } |
| 81 | + close(i); |
| 82 | + } |
| 83 | + console.log("Namaste JS"); |
| 84 | +} |
| 85 | +x(); |
| 86 | +``` |
| 87 | + |
| 88 | +**Why this works?** |
| 89 | + |
| 90 | +We enclose the `setTimeout` inside a `close()` function. And everytime in a loop, we call `close(i)` which passes a unique value of `i` to each time `setTimeout` is called as `x` is already inside the lexical environment. |
| 91 | + |
| 92 | +In this way the problem is tackled. |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
0 commit comments