简体   繁体   English

我可以使用带有箭头 function 的生成器函数吗?

[英]Can I use generator-function with arrow function?

I wander if it's possible to use arrow function with generators like this:我想知道是否可以将箭头 function 与这样的生成器一起使用:

app.use( *() => {
    ...body of function
});

Thanks!谢谢!

No, it's not possible to make it shorter than described in the documentation: 不可以,它不可能比文档中描述的要短:

function* name([param[, param[, ... param]]]) {
   statements
}

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* 参考: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/function*

You can, but not really in a nice way.你可以,但不是很好。 It's not shorter and does not look that pretty.它不短,看起来也不那么漂亮。 Check this out:看一下这个:

function* iterable(arg): Iterable<never> {
    yield* [];
}

async function* asyncIterable(arg): AsyncIterable<never> {
    yield* [];
}

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

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

This works because an iterable is basically an object with the Symbol.iterator or Symbol.asyncIterator set to an iterator.这是可行的,因为可迭代对象基本上是一个 object,其中Symbol.iteratorSymbol.asyncIterator设置为一个迭代器。 A generator is an iterator!生成器是迭代器!

Enjoy!享受!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Tcpsocket 中使用箭头 function? - How can i use arrow function in Tcpsocket? 我可以使用 function 生成器作为事件处理程序吗? - Can I use function generator as event handler? 如何在函数组件(胖箭头函数)中使用构造函数? - how can i use constructor in function component (fat arrow function)? 为什么在超级构造函数的调用中不能在箭头函数中使用“ this”? - Why can't I use “this” in an arrow function in a call to the super constructor? 我可以在 React 组件的构造函数中使用箭头函数吗? - Can I use arrow function in constructor of a react component? 我可以在Vue中使用带有箭头功能的样式绑定吗? - Can I use style binding with arrow function in Vue? 我可以在箭头函数中调用这个 function 吗? - Can I call this function in an arrow-function? 我可以在生成器中使用 ES6 的箭头 function 语法吗? (箭头符号) - Can I use ES6's arrow function syntax with generators? (arrow notation) 我可以在要传递到循环中的箭头函数的参数中使用for循环迭代器吗? - Can I use a for loop iterator in the parameter of an arrow function to be passed into the loop? 不能使用速记箭头功能 - Can't use shorthand arrow function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM