
What does `return function *(){…}` mean in JavaScript? - afshinmeh
http://stackoverflow.com/q/19833945/375966
======
mhitza
The asterix after the function keyword smells like a syntax hack. Firefox did
it right, [https://developer.mozilla.org/en-
US/docs/Web/JavaScript/New_...](https://developer.mozilla.org/en-
US/docs/Web/JavaScript/New_in_JavaScript/1.7)

~~~
masklinn
Depends what you mean by "syntax hack". Its point is to create a syntactic
scope where `yield` can not currently exist and can be reserved. Since yield
is a valid name in javascript today there might be code in the wild going

    
    
        function foo() {
            yield = some_computation();
            return yield;
        }
    

this code would be broken by enabling generators. Same with yield* foo()
(which would be interpreted as multiplying the variable `yield` by the result
of `foo()` in ES5).

It also creates a nice parallelism between function* and yield _, as a side-
effect.

See [http://esdiscuss.org/topic/proposal-to-simplify-
generators-i...](http://esdiscuss.org/topic/proposal-to-simplify-generators-
impact#content-1), there should be more extensive previous discussions in es-
discuss and TC39 notes but it's a pain in the ass to look it up.

edit: [https://mail.mozilla.org/pipermail/es-
discuss/](https://mail.mozilla.org/pipermail/es-discuss/) also points to the
use case of 0-item generators — mostly in relation to delegating from yield_ —
with

    
    
        function* () {}
    

in Python (and JS/1.7) you'd need the unwieldy

    
    
        function() {
            if (false) yield
        }
    

and notes that having a star in the header function makes it easier on the
reader.

~~~
apaprocki
> ... there might be code in the wild going ...

There definitely is. We have millions of lines of JS and many applications
that deal with yield of a security. _This_ yield:
[http://www.investopedia.com/terms/y/yield.asp](http://www.investopedia.com/terms/y/yield.asp)

~~~
masklinn
Excellent point.

------
bdfh42
But jump to this [http://wingolog.org/archives/2013/05/08/generators-
in-v8](http://wingolog.org/archives/2013/05/08/generators-in-v8) which
explains it properly - and tells you why you would want to do it.

~~~
k__
Does it need to be used with promises?

~~~
apaprocki
No. Promises work nicely with generators, but you can use generator functions
freely.

~~~
k__
Is there any promise-less guide, that helps to grasp the concept of function*
and yield in JS?

~~~
masklinn
Any generator guide for Python will work, the syntax is slightly different (no
function _/ def_, the generator is "created" by the presence of a `yield`
keyword in the body, and `yield*` is called `yield from`)

~~~
k__
The easiest thing to do is probably playing around with it :)

------
TillE
Am I missing something, or does Stack Overflow really have this marked as a
duplicate without actually linking to the earlier question? That's not very
helpful.

~~~
antsar
Not sure if it was there when you looked, but I see a link[1] to the relevant
question in a grey box at the top of the question. I find the placement to be
awkward though, I expected the link to be within the same box as the "marked
as duplicate" text.

[1] [http://stackoverflow.com/questions/9620586/function-in-
javas...](http://stackoverflow.com/questions/9620586/function-in-javascript)

~~~
smackfu
It doesn't stand out very much in this question because it has a big block of
code which has the same styling as the alert box. When your interface design
has so few colors, this kind of stuff happens.

