|
| 1 | +{ |
| 2 | + "lessonId": "js-9-2", |
| 3 | + "title": "Generators", |
| 4 | + "concept": "A generator is a special function that can pause execution in the middle (Yield) and resume it later (Resume).", |
| 5 | + "content": { |
| 6 | + "code": "function* counter() {\n console.log('Start!');\n yield 1; // Pause execution (return value 1)\n console.log('Resumed!');\n yield 2; // Pause execution (return value 2)\n console.log('Done!');\n}\n\n// Calling the function doesn't run it — it returns an Iterator (control object)\nconst gen = counter();\n\nconsole.log(gen.next()); // { value: 1, done: false }\nconsole.log(gen.next()); // { value: 2, done: false }\nconsole.log(gen.next()); // { value: undefined, done: true }", |
| 7 | + "steps": [ |
| 8 | + { |
| 9 | + "title": "Defining a generator function", |
| 10 | + "explanation": "The `function*` syntax marks this as a generator function.\n\n**What is a generator?**\nA regular function runs to completion once called (Run-to-completion), but a generator can pause partway through and resume later.", |
| 11 | + "visualizationType": "memory", |
| 12 | + "memoryState": { |
| 13 | + "stack": [ |
| 14 | + { |
| 15 | + "name": "counter", |
| 16 | + "value": "GeneratorFunction" |
| 17 | + } |
| 18 | + ] |
| 19 | + }, |
| 20 | + "code": "function* counter() {", |
| 21 | + "highlightOffset": [ |
| 22 | + 0, |
| 23 | + 1, |
| 24 | + 2, |
| 25 | + 3, |
| 26 | + 4, |
| 27 | + 5, |
| 28 | + 6 |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "title": "Creating the generator object", |
| 33 | + "explanation": "Calling `counter()` does not run the function body — instead, it returns a **Generator object (Iterator)**.\nThis object gives you control over when the function executes.", |
| 34 | + "keyInsight": "Calling a generator function = creating an Iterator, not running the function", |
| 35 | + "visualizationType": "memory", |
| 36 | + "memoryState": { |
| 37 | + "stack": [ |
| 38 | + { |
| 39 | + "name": "gen", |
| 40 | + "value": "Generator { status: 'suspended' }" |
| 41 | + } |
| 42 | + ] |
| 43 | + }, |
| 44 | + "code": "const gen = counter();" |
| 45 | + }, |
| 46 | + { |
| 47 | + "title": "First next() call", |
| 48 | + "explanation": "Calling `gen.next()` starts the function for the first time.\n'Start!' is logged, then execution pauses at `yield 1`.\n\n**What is Yield?**\nA keyword that pauses execution — handing control and a value back to the caller while keeping the execution context intact.", |
| 49 | + "visualizationType": "memory", |
| 50 | + "memoryState": { |
| 51 | + "output": [ |
| 52 | + "시작!", |
| 53 | + "{ value: 1, done: false }" |
| 54 | + ], |
| 55 | + "heap": [ |
| 56 | + { |
| 57 | + "address": "0xGen", |
| 58 | + "content": "Generator { suspended at line 3 }" |
| 59 | + } |
| 60 | + ] |
| 61 | + }, |
| 62 | + "code": "console.log(gen.next()); // { value: 1, done: false }", |
| 63 | + "highlightOffset": [ |
| 64 | + 0, |
| 65 | + -10, |
| 66 | + -9 |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "title": "Second next() call", |
| 71 | + "explanation": "Calling `gen.next()` again resumes from just after `yield 1`.\n'Resumed!' is logged, then execution pauses again at `yield 2`.", |
| 72 | + "visualizationType": "memory", |
| 73 | + "memoryState": { |
| 74 | + "output": [ |
| 75 | + "재개!", |
| 76 | + "{ value: 2, done: false }" |
| 77 | + ], |
| 78 | + "heap": [ |
| 79 | + { |
| 80 | + "address": "0xGen", |
| 81 | + "content": "Generator { suspended at line 5 }" |
| 82 | + } |
| 83 | + ] |
| 84 | + }, |
| 85 | + "code": "console.log(gen.next()); // { value: 2, done: false }", |
| 86 | + "highlightOffset": [ |
| 87 | + 0, |
| 88 | + -9, |
| 89 | + -8 |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "title": "Third next() call — completion", |
| 94 | + "explanation": "The final `gen.next()` call.\n'Done!' is logged, and since there are no more yields, `done: true` is returned.", |
| 95 | + "keyInsight": "done: true = the generator has finished executing completely", |
| 96 | + "visualizationType": "memory", |
| 97 | + "memoryState": { |
| 98 | + "output": [ |
| 99 | + "끝!", |
| 100 | + "{ value: undefined, done: true }" |
| 101 | + ], |
| 102 | + "heap": [ |
| 103 | + { |
| 104 | + "address": "0xGen", |
| 105 | + "content": "Generator { closed }" |
| 106 | + } |
| 107 | + ] |
| 108 | + }, |
| 109 | + "code": "console.log(gen.next()); // { value: undefined, done: true }", |
| 110 | + "highlightOffset": [ |
| 111 | + 0, |
| 112 | + -8 |
| 113 | + ] |
| 114 | + } |
| 115 | + ] |
| 116 | + }, |
| 117 | + "quiz": { |
| 118 | + "question": "What signal indicates that a generator function has fully finished executing?", |
| 119 | + "options": [ |
| 120 | + "value: null", |
| 121 | + "done: true", |
| 122 | + "status: finished", |
| 123 | + "yield: undefined" |
| 124 | + ], |
| 125 | + "correctIndex": 1, |
| 126 | + "explanation": "When the IteratorResult object `{ value: ..., done: true }` is returned, it means there is no more code left to run." |
| 127 | + }, |
| 128 | + "misconceptions": [ |
| 129 | + { |
| 130 | + "wrong": "Generators run concurrently like multiple threads", |
| 131 | + "correct": "No — they still run on a single thread.", |
| 132 | + "why": "Generators only pass execution control back and forth (Cooperative Multitasking) — they don't run simultaneously.\nOnly one thing executes at a time." |
| 133 | + } |
| 134 | + ], |
| 135 | + "keyTakeaway": "Because you can pause and resume function execution, generators are useful for writing asynchronous code that reads like synchronous code (e.g., Redux-Saga)." |
| 136 | +} |
0 commit comments