Hope you found this video helpful! What topic would you like to learn next? Drop it in the comments below and I might make a video about it Also, visit www.reactsquad.io/ now and hire your next senior React developer today!
came here from your blog I don't understand why it printed "y: 2012", should it be "y: 2014" (the value in parameter was 2012 + 2) or "y: 42" (as this was the result of y from previous call)
Let me explain to you the output of the given code step by step: 1. Initialization of the generator: When `moreNumbers(40)` is called, the generator function is initialized with `x = 40`, but no code runs yet. 2. First `next()` call: This starts the execution of the generator until the first `yield`. It prints `'x', 40` and then yields `40 + 2` (i.e., 42). The output is: { value: 42, done: false } 3. Second `next(2012)` call: This resumes the generator, passing `2012` back to it. This value is assigned to `y`. It then prints `'y', 2012` and yields `40 + 2012` (i.e., 2052). The output is: { value: 2052, done: false } 4. Third `next()` call: This resumes the generator again, but since no value is passed, `z` is `undefined`. It prints `'z', undefined` and as there are no more `yield` statements, the generator finishes. The output is: { value: undefined, done: true } The sequence of console logs from your code would look like this: - `'x', 40` - `{ value: 42, done: false }` - `'y', 2012` - `{ value: 2052, done: false }` - `'z', undefined` - `{ value: undefined, done: true }` The formatting here on YT might be weird, but you can copy paste it in a markdown file. Hope this helps!
@@JanHesters ahh thanks 🤟, so basically when we call next(value) it doesn't re-initialize the value of x, but assign this value to the variable that was assign to the last yield. and doing this const y = yield x + 2; // x = 40 doesn't assign the result (42) to y
Hope you found this video helpful!
What topic would you like to learn next? Drop it in the comments below and I might make a video about it
Also, visit www.reactsquad.io/ now and hire your next senior React developer today!
Thank you Jan
Let's go 🔥
Generators are super interesting, I am curious about the Saga video!
Thank you! Coming soon 🔥
came here from your blog
I don't understand why it printed
"y: 2012",
should it be
"y: 2014" (the value in parameter was 2012 + 2)
or
"y: 42" (as this was the result of y from previous call)
Let me explain to you the output of the given code step by step:
1. Initialization of the generator: When `moreNumbers(40)` is called, the generator function is initialized with `x = 40`, but no code runs yet.
2. First `next()` call: This starts the execution of the generator until the first `yield`. It prints `'x', 40` and then yields `40 + 2` (i.e., 42). The output is:
{ value: 42, done: false }
3. Second `next(2012)` call: This resumes the generator, passing `2012` back to it. This value is assigned to `y`. It then prints `'y', 2012` and yields `40 + 2012` (i.e., 2052). The output is:
{ value: 2052, done: false }
4. Third `next()` call: This resumes the generator again, but since no value is passed, `z` is `undefined`. It prints `'z', undefined` and as there are no more `yield` statements, the generator finishes. The output is:
{ value: undefined, done: true }
The sequence of console logs from your code would look like this:
- `'x', 40`
- `{ value: 42, done: false }`
- `'y', 2012`
- `{ value: 2052, done: false }`
- `'z', undefined`
- `{ value: undefined, done: true }`
The formatting here on YT might be weird, but you can copy paste it in a markdown file.
Hope this helps!
@@JanHesters ahh thanks 🤟,
so basically when we call next(value) it doesn't re-initialize the value of x, but assign this value to the variable that was assign to the last yield.
and doing this
const y = yield x + 2; // x = 40
doesn't assign the result (42) to y