Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generator without yield

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?

like image 425
Alkis Kalogeris Avatar asked Dec 21 '15 21:12

Alkis Kalogeris


People also ask

Does generator function need yield?

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.

What is yield in generator?

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.

Does yield return a generator?

The yield statement returns a generator object to the one who calls the function which contains yield, instead of simply returning a value.

What is the use of yield?

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.


1 Answers

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.

like image 72
Bergi Avatar answered Sep 22 '22 02:09

Bergi