ES6 yield:第一次调用next()的参数发生了什么? [英] ES6 yield : what happens to the arguments of the first call next()?

查看:408
本文介绍了ES6 yield:第一次调用next()的参数发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码段:

function foo(a) {
  console.log("Mul =", a);
  return a * 2;
};

function * process(start) {
  // next() #1
  var result = start;

  console.log("Pre-processing =", result);
  result = yield foo(result);
  // next() #2
  console.log("Process result 1 =", result);
  result = yield foo(result);
  // next() #3
  console.log("Process result 2 =", result);
  result = yield foo(result);
  // next() #4
  console.log("Process result 3 =", result);

  return foo(result);
}

var it = process(1);
console.log("#1");
console.log("Next 1 =", /*#1*/it.next("bar"));
console.log("#2");
console.log("Next 2 =", /*#2*/it.next(3));
console.log("#3");
console.log("Next 3 =", /*#3*/it.next(7));
console.log("#4");
console.log("Next 4 =", /*#4*/it.next(15));

和输出

#1
Pre-processing = 1
Mul = 1
Next 1 = { value: 2, done: false }
#2
Process result 1 = 3
Mul = 3
Next 2 = { value: 6, done: false }
#3
Process result 2 = 7
Mul = 7
Next 3 = { value: 14, done: false }
#4
Process result 3 = 15
Mul = 15
Next 4 = { value: 30, done: true }

为什么第一次调用it.next()会完全跳过跳过参数(在上面的代码中,在"bar"中)?或者换句话说,为什么后续调用中的行为不同?我曾期望调用generator函数会跳过参数,而对next()的调用实际上会初始化迭代器,从而使过程更加一致,不是吗?

Why is the first call to it.next() skip arguments (in the code above, "bar") altogether? Or, in other words, why is the behavior different in subsequent calls? I would've expected calling the generator function would skip arguments, and that the call to next() would actually initialize the iterator, making the process more coherent, no?

推荐答案

在草稿中:

经过更多研究后,答案就在和睦的草案中(请参阅Wiki:

After some more research, the answer lies within harmony's draft (see the wiki: http://wiki.ecmascript.org/doku.php?id=harmony:generators#methodnext).

next应该没有参数.但是,似乎只用一个参数调用next等效于只用一个参数调用send.答案就在这里.如果首先调用send,则抛出错误(没有next优先).

next is supposed to have no argument. However, it seems calling next with one argument is just equivalent to call send with one argument. Here lies the answer. send is designed to throw an error if called first (no next prior).

因此,基本上,您不应通过将参数传递给next来初始化"您的迭代器,因为您无权这样做.

So basically, you should not be able to "initialize" your iterator by passing an argument to next cause you're not authorized to do so.

在实现中:

但是,这只是规范.总结一下所说的评论,至少有两个原因导致您无法将参数传递给第一个next,而必须将其传递给生成器.

However, this is just the specification. To summarize what's been said as comments, there are at least 2 reasons why you can't pass an argument to your first next and have to pass it to your generator.

第一个事实是您需要某种方法来实际获得此参数.您不能以与下次呼叫let myVar = yield myValue相同的方式进行此操作.
第二个问题是next仅接受一个参数,这是非常有限的,而在生成迭代器时,您可以将无限数量的参数传递给生成器.

The first one would be the fact that you would need some method to actually get this argument. You cannot do it the same way you'd do it with your next calls let myVar = yield myValue.
The second one would be that next only accepts one argument and that is quite limiting, whereas you can pass an infinite amount of arguments to your generator when producing the iterator.

但是,这只是此刻正在发生的事情.没什么说草案或实现不会改变.我们当然可以想象send接受任何数量的参数(没有理由,但是,嘿,谁知道),并能够将其转换为生成器的参数.还是什么.

However, this is only what's happening at the moment. Nothing says that the draft or implementations won't change. We could certainly imagine send accepting any number of arguments (no reason but hey, who knows), and being able to cast it into the generator's arguments. Or whatever.

这篇关于ES6 yield:第一次调用next()的参数发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆