也就是说,我怎么表达

function *(next) {}

用箭头语法?我已经尝试了所有我能想到的组合,但我找不到任何关于它的文档。

(我目前使用的是Node.js v0.11.14。)


当前回答

I know that this is very late, but another possible reason could be syntax. maybe (*() => {}) works, but what about (9 ** () => {})? Is that 9 to the power of an arrow function, returning NaN, or is it 9 times a generator arrow function, also returning NaN? It could be done with some alternative syntax, like =>* as mentioned by another answer here, but maybe there was a desire to preserve the consistency of the generator function syntax (eg. function* () {} and { *genMethod() {} }) when it was being implemented. Not too much of an excuse, but a reason for it.

其他回答

我可以使用ES6的箭头函数语法生成器吗?

你不能。对不起。

根据MDN

function*语句(function关键字后面带星号)定义了一个生成器函数。

从一个规范文档(我的重点):

函数语法被扩展为添加一个可选的*令牌:

FunctionDeclaration: "function" "*"? Identifier "(" FormalParameterList? ")" 
  "{" FunctionBody "}"

你可以,但不是用好的方式。它不短,看起来也不漂亮。看看这个:

function* iterable(arg) {
    yield* [];
}

async function* asyncIterable(arg) {
    yield* [];
}

const arrowIterable = arg => {
    return {
        *[Symbol.iterator]() {
            yield* [];
        },
    };
};

const arrowAsyncIterable = arg => {
    return {
        async *[Symbol.asyncIterator]() {
            yield* [];
        },
    };
};

这是可行的,因为iterable基本上是一个带有Symbol的对象。迭代器或符号。asyncIterator设置为一个迭代器。生成器是迭代器!

享受吧!

内联函数和箭头函数的区别

首先,Arrow-functions() =>{}不是用来替换内联函数function(){},它们是不同的。 内联函数就是简单的函数,所以问题是箭头函数和内联函数之间的区别是什么。

与函数表达式相比,箭头函数表达式(也称为箭头函数)具有更短的语法,并且不绑定自己的this、参数、super或new.target)。箭头函数总是匿名的。

这里有更多的细节


为什么箭头函数不能用作生成器

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

使用yield关键字 yield关键字不能在箭头函数体中使用(除非允许在箭头函数体中进一步嵌套的函数中使用)。因此,箭头函数不能用作生成器。

注意,没有yield的生成器是没有意义的。


为什么箭函数不能使用yield

http://tc39wiki.calculist.org/es6/arrow-functions/

箭头函数在词法上绑定它,在Block体情况下绑定return,以便它从直接封闭的箭头函数返回,并排除break和continue引用直接封闭的箭头函数之外的语句。 Identifier主表达式参数不能在箭头函数体中使用(无论是表达式形式还是块形式)。 同样,yield也不能用在箭头函数体中。箭头不能作为生成器,我们也不想要深度延续。

Yield在一个箭头函数将抛出语义错误:http://www.ecma-international.org/

究其原因,在于ECMA6实现的深度复杂性。由于类似的原因,c#也不允许这样做。

redux-saga有一个很好的解决方案

import { call, all } from 'redux-saga/effects';

function* gen() {
   yield all([].map(() => {
      return call(....);
   }));
}

I know that this is very late, but another possible reason could be syntax. maybe (*() => {}) works, but what about (9 ** () => {})? Is that 9 to the power of an arrow function, returning NaN, or is it 9 times a generator arrow function, also returning NaN? It could be done with some alternative syntax, like =>* as mentioned by another answer here, but maybe there was a desire to preserve the consistency of the generator function syntax (eg. function* () {} and { *genMethod() {} }) when it was being implemented. Not too much of an excuse, but a reason for it.