- Overview
- Transcript
6.5 Generator Functions
Generators are a special type of function that can be paused during execution using a yield
expression. Let’s learn how to pass values in to generators, and see what comes out when we use them.
Syntax
function*
yield
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
6.5 Generator Functions
Hi folks, in this lesson we're going to take a look at another type of function that we can use in JavaScript. A generator function. This type of function is very powerful, but is very different from regular JavaScript functions. Defining a generator function is really easy. We need to use the function keyword with this type of function. We just need to add an asterisk after it. In almost all respects, it's just like a regular function, it can have a name, and it can accept parameters. With a regular function, however, once it starts, it executes all of the statements it contains and then returns. All further activity of the function stops, unless the function gets invoked again, in which case it basically starts again. Generator functions, on the other hand, do not run to completion. They can return a value, pause, and then be resumed at any time later. And they pick up exactly where they left off and continue processing, sometimes returning many values before they actually end. Regular functions are blocking, while they are running, no other code can run. With generator functions we can pause them. And this allows other codes to run. And we can then come back and resume the generator function later on. So generator functions are only blocking while they are actually running, not all the time. The way that we pause a generator, is with the yield keyword. We can use yield by itself, or we can use it to return a value. A bit like the return keyword. And like with the return keyword, if we don't specify a return value, it will return an implicit undefined. So let's update our generator function now to make use of the yield keyword. And just so that you can be sure that the generator function does pause after this statement, let's add a console log directly after it Another difference between regular functions and generator functions is that we don't actually invoke generator functions like we do regular functions. We invoke the generator in order to get an iterator. And it is the iterator which actually invokes the generator. We still need to pass the parameter that the generator function is invoking when we invoke it to get back the iterator. We looked at iterators earlier in the course and we saw that they should have a next method. So in order to invoke the generator function, we need to invoke the next method of the iterator that invoking the generator returned. So at this point, we have invoked the generator in order to get back the iterator and we've invoked the next method of the iterator. So the generator function should now have started running. Let's just check the output in the browser quickly. We can see that we don't see that log message that we added after the yield keyword. So what's actually happened is when we invoked the next method, the generator function ran until it encountered the yield statement, which happened to be the first statement inside the function in this example. And then it paused. As soon as the x multiplied by x value was yielded, the function paused. So let's just wrap a console log around where we invoke the next method. So now we can see that invoking the next method resulted in an object being returned. And like the object that is returned with other iterators, it contains a value and a done property. So the value contains whatever the yield keyword returned, which in this case is the number 25. And is also has this done property saying false. And what that means is that even though the generator function has been invoked, it still contains more expressions which can be evaluated. So let's invoke it once again. So now we see the log message that was added after the yield keyword. So we can see that the generator function was paused after it yielded. And that when we invoke the next method a second time, we saw the log message that came after the yield expression. And this time in the object that gets returned from invoking the next method, we can see that done is true and this means that there are no more expressions to evaluate within the generator function. So if we invoke the next method again, nothing will happen. If we want to run the generator again, we'll need to get a new iterator and start again basically. So one of the key points of this example so far is how we can pass a value back from the generator. And we do that, as we saw, using the yield expression. But what about sending new values into the generator? This is also possible and is another fundamental aspect to generators. So let's just go back and modify the original generator functions slightly. And let's also log this new variable to the console in the log statement. And let's also make a change to how we use this function. So when we first get the iterator, we can continue passing 5 in. But let's use the next method of the iterator to pass a different value into the generator when it get resumed. So this time we can see that something interesting has happened. So this time we passed the value of 10 into the generator using the next method the second time we invoke next. And we can see that the output is now changed. So when the generator runs and evaluates the first yield expression, it will still return 25 like it did before. But when we pass 10 in the second time the next method is invoked, the yield expression will be replaced with this new value. So when we invoke the generator, we still pass 5 in. And so, 5 is the value of x when the generator function first begins to run, but then when we invoke the next method and pass 10 in, the yield expression, inside parenthesis, will be replaced with this value that we pass in. So, y then becomes 10 multiplied by 5. As well as using yield, we can can also use return at the end of the generator. And when the iterator for the generator is used with the next method, the value returned will be the value property in the object returned by the next method. Another point to know is that a value passed to the next method, the first time next method is used, is discarded. And it's not available inside the generator, the reason for this is because the generator hasn't yet evaluated a yield expression the very first time it gets invoked. So in this lesson, we learned about generator functions. And so that they are a powerful type of new function that can be paused and resumed, instead of running to completion and blocking other code. We saw that we define a generator using the star character after the function keyword. We learned that we can pause a generator using the yield keyword. And that we can pass a value out of the function using this yield keyword. We also saw that generator functions are used with an iterator. We invoke the generator once to get its iterator and we then use the iterator's next method to start or resume the generator. We also saw that we can pass values into a generator when resuming it using the next method. And that these values will be inserted as the result of the yield expression that the generator previously paused at. In the next lesson, we're going to look at closures in JavaScript. Thanks for watching.