访问内部的箭头功能 [英] accessing yield inside arrow function
问题描述
因为它不可能创建一个箭头生成函数,
yield 在箭头函数的上下文中永远不会使用。
since it it not possible to create a arrow generator function, yield is never used in the context of a arrow function.
var arrowGenerator = *() => { };
那么你应该能够在上下文中使用 yield 发电机功能。就像这个
then you should be able to and use yield in the context of the generator function. just like this
function* generator() {
funcWithCallback((value) => {
yield value;
});
}
但是在babel中,它使用 yield 箭头功能而不是发音器。
but in babel it uses yield in context of the arrow function and not the genreators.
我想这样做,所以你不需要返回一个回调函数的值,只是为了产生它。
i want to do this so you don't need to return a callback function with the value, just to yield it.
function* gen() {
yield function (callback) {
funcWithCallback(callback);
}
}
推荐答案
收益
和 yield *
关键字只能直接使用 里面有发电机功能。您的代码片段在概念上有缺陷,类似于以下方式:
The yield
and yield*
keywords can only be used directly inside a generator function. Your code fragment is conceptually flawed in a similar manner to:
function f1() {
if(someCondition) {
f2((value) => {
else {
// do something
}
});
}
}
或,为此:
function f1() {
f2((value) => {
return someValue; // while this is legal, it doesn't cause f1 to return
});
codeAfterReturn();
}
显然,这两个例子不工作,你的代码片段。
Obviously, these 2 examples don't "work", and so does your code fragment.
这篇关于访问内部的箭头功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!