JavaScript 中奇怪的 Yield 语法

发布于 2024-11-29 00:52:20 字数 453 浏览 2 评论 0原文

我刚刚看了 Dave Herman 非常有趣的 task.js。在他的例子中,他有这样一行:

var [foo, bar] = yield join(read("foo.json"),
                                read("bar.json")).timeout(1000);

我熟悉生成器,但我不明白yield表达式如何计算为可以分配给[foo, bar]的东西。实际上,我没想到该表达式可以分配给任何东西,因为它基本上与 return 相同。

JS 的 Yield 语法似乎仍然缺乏文档,我找不到有关此的信息。

因此,为了澄清我的问题:最终分配给 foo 和 bar 的是什么?

I just took a look at Dave Herman's very interesting task.js. In his example he has this line:

var [foo, bar] = yield join(read("foo.json"),
                                read("bar.json")).timeout(1000);

I'm familiar with generators but I don't understand how the yield expression evaluates to something that can be assigned to [foo, bar]. I actually wouldn't have expected the expression to be assignable to anything since it is basically the same thing as return.

The yield syntax for JS still seems a bit underdocumented and I couldn't find info about this.

So to clarify my question: what ends up being assigned to foo and bar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光暖心i 2024-12-06 00:52:20

实际上,相关段落位于 https://developer.mozilla.org/En/New_in_JavaScript_1.7

一旦通过调用 next() 方法启动了生成器,您就可以
可以使用 send(),传递一个特定的值,该值将被视为
最后一次yield的结果。然后生成器将返回操作数
后续产量

我认为这仅在通过直接调用其方法来使用生成器而不是循环其值时才相关 - 循环将始终在生成器上调用 next() 并且从不 send( )

在某种程度上,生成器执行类似于协作多任务处理。生成器一直执行,直到找到 yield 语句。它将控制权返回给在生成器上调用 next()send() 的人。然后调用者继续执行,直到执行下一个 next()send() 调用 - 现在生成器再次执行。每个时间值都可以来回传递。

这是一个简单的例子:

function gen()
{
  var [foo, bar] = yield 1;
  console.log("Generator got: " + foo + ", " + bar);
}

// This creates a generator but doesn't run it yet
var g = gen();

// Starts generator execution until a yield statement returns a value
var result = g.next()
console.log("Received from generator: " + result);

// Continue generator execution with [2, 3] being the return value
// of the yield statement. This will throw StopIteration because the
// iterator doesn't have any more yield statements.
g.send([2, 3]);

Actually, the relevant paragraph is a little below in https://developer.mozilla.org/En/New_in_JavaScript_1.7:

Once a generator has been started by calling its next() method, you
can use send(), passing a specific value that will be treated as the
result of the last yield. The generator will then return the operand
of the subsequent yield.

I think that this is only relevant if the generator is used by calling its methods directly, not when looping over its values - a loop will always call next() on the generator and never send().

In a way, generator execution is similar to cooperative multitasking. The generator executes until the yield statement is found. It returns control to whoever called next() or send() on the generator. The caller then continues executing, until the next next() or send() call is performed - now the generator is executing again. Each time values can be passed back and forth.

Here a simple example:

function gen()
{
  var [foo, bar] = yield 1;
  console.log("Generator got: " + foo + ", " + bar);
}

// This creates a generator but doesn't run it yet
var g = gen();

// Starts generator execution until a yield statement returns a value
var result = g.next()
console.log("Received from generator: " + result);

// Continue generator execution with [2, 3] being the return value
// of the yield statement. This will throw StopIteration because the
// iterator doesn't have any more yield statements.
g.send([2, 3]);
谜兔 2024-12-06 00:52:20

https://developer.mozilla.org/En/New_in_JavaScript_1.7

包含yield关键字的函数是生成器。当您调用它时,它的形式参数绑定到实际参数,但它的主体实际上并没有被评估。相反,返回一个生成器迭代器。每次调用生成器迭代器的 next() 方法都会执行迭代算法的另一遍。每个步骤的值是由yield 关键字指定的值。将yield视为return的生成器迭代器版本,指示算法每次迭代之间的边界。每次调用 next() 时,生成器代码都会从ield后面的语句恢复。

https://developer.mozilla.org/En/New_in_JavaScript_1.7

The function containing the yield keyword is a generator. When you call it, its formal parameters are bound to actual arguments, but its body isn't actually evaluated. Instead, a generator-iterator is returned. Each call to the generator-iterator's next() method performs another pass through the iterative algorithm. Each step's value is the value specified by the yield keyword. Think of yield as the generator-iterator version of return, indicating the boundary between each iteration of the algorithm. Each time you call next(), the generator code resumes from the statement following the yield.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文