5 ms·
What does `return function *(){…}` mean in JavaScript?
- bdfh42 13y agoBut 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.
- afshinmeh 13y agoagreed
- k__ 13y agoDoes it need to be used with promises?
- apaprocki 13y agoNo. Promises work nicely with generators, but you can use generator functions freely.
- k__ 13y agoIs there any promise-less guide, that helps to grasp the concept of function* and yield in JS?
- Touche 13y agoNot an article, but this is a good library that uses generator functions without promises: https://github.com/visionmedia/co https://github.com/visionmedia/co
- masklinn 13y agoAny 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__ 13y agoThe easiest thing to do is probably playing around with it :)
- TillE 13y agoAm 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 13y agoNot 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-javascript http://stackoverflow.com/questions/9620586/function-in-javas...
- smackfu 13y agoIt 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.
- cruise02 13y agoThat's inserted automatically when questions are closed as duplicate.
- deleted 13y ago[deleted]
- mhitza 13y agoThe asterix after the function keyword smells like a syntax hack. Firefox did it right, https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7 https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_...
- masklinn 13y agoDepends 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-impact#content-1 http://esdiscuss.org/topic/proposal-to-simplify-generators-i..., 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.
- deleted 13y ago[deleted]
- ynniv 13y agoWhy would yield as an lvalue or var conflict with yield as a statement? Yes it would be unavoidable if a variable named yield was used as a statement, but that's a pretty strange thing to do. Reserved words are unnecessary. As to convenient empty generators, they don't need to be enabled by syntax. You could define a function empty_gen() that is even fewer characters than function* () {}.
- 13y ago