var A = {
demo : function() * {
/* Some logic here, but no yield is used */
}
}
What is the use of a generator
method that does not yield
anything?
Have you ever used something like this? What was the use case?
A generator function is like a normal function, instead of having a return value it will have a yield keyword. To create a generator function you will have to add a yield keyword. The following examples shows how to create a generator function.
The yield keyword pauses generator function execution and the value of the expression following the yield keyword is returned to the generator's caller. It can be thought of as a generator-based version of the return keyword. yield can only be called directly from the generator function that contains it.
The yield statement returns a generator object to the one who calls the function which contains yield, instead of simply returning a value.
yield keyword is used to create a generator function. A type of function that is memory efficient and can be used like an iterator object. In layman terms, the yield keyword will turn any expression that is given with it into a generator object and return it to the caller.
It's quite the same case like an empty function - someone wants to call a function, but you have nothing to do.
Similarly, an empty generator function is a function which creates a generator that does nothing. It does represent the empty sequence. However, a generator function that doesn't yield
isn't necessarily empty - it can still do something and have a result value, but there simply are no intermediate results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With