[英]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.iterator
或Symbol.asyncIterator
设置为一个迭代器。 A generator is an iterator!生成器是迭代器!
Enjoy!享受!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.