Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于 ECMA6 对 Generator 运行结束后再调用 next 的Issue #9

Closed
yleo77 opened this issue May 11, 2014 · 5 comments
Closed

关于 ECMA6 对 Generator 运行结束后再调用 next 的Issue #9

yleo77 opened this issue May 11, 2014 · 5 comments

Comments

@yleo77
Copy link

yleo77 commented May 11, 2014

文档 中描述

由于此时函数已经运行完毕,next方法直接抛出一个错误。

我测试了一下,在 firefox 版本 29 和 nodejs 上,获取到的结论是不会抛错,返回值为

{value: undefined, done: true}

简单跟了一下,翻了下相关资料,firefox 原本是抛出异常,但这个被视为一个 bug 在 firefox 29 版本中已经修复

相关源码见 1, 2

另外,MDN 上这篇文章 给出了一个不同的写法,也就是没带*****号. 具体看他们的代码:

function simpleGenerator(){ //注意这里,没有*
    yield "first";
    yield "second";
    yield "third";
    for (var i = 0; i < 3; i++)
    yield i;
}
var g = simpleGenerator();
print(g.next()); // prints "first"
print(g.next()); // prints "second"
print(g.next()); // prints "third"
print(g.next()); // prints 0
print(g.next()); // prints 1
print(g.next()); // prints 2
print(g.next()); // StopIteration is thrown

对于这种写法的 case,会抛出异常uncaught exception: [object StopIteration]

这种写法抛出异常属于正确行为,因为它本身是函数,只是因为 yield 关键字的存在能够达到中断执行函数的目的,当函数执行完毕再调用 next 抛出异常可以理解。

但是就 Generator 来说,个人观点是,已经完成的遍历器再调用 next,不管调用多少次依然返回{done:true} 从语义上来说更合理些

否则日后想要判断一个遍历器是否完成都比较繁琐了,当第一次调用 next 完成时返回 {done: true} ,以后再调用都抛错这种机制也稍显混乱。

请阮兄这里再确认一下。

@ruanyf
Copy link
Owner

ruanyf commented May 13, 2014

我在Node v0.11.11中试过了,确实第四次next方法会抛出一个错误。

> hw.next() 
{ value: 'hello', done: false }
> hw.next() 
{ value: 'world', done: false }
> hw.next() 
{ value: undefined, done: true }
> hw.next() 
Error: Generator has already finished
    at GeneratorFunctionPrototype.next (native)
    at repl:1:4
    at REPLServer.defaultEval (repl.js:129:27)
    at bound (domain.js:255:14)
    at REPLServer.runBound [as eval] (domain.js:268:12)
    at Interface.<anonymous> (repl.js:277:12)
    at Interface.EventEmitter.emit (events.js:104:17)
    at Interface._onLine (readline.js:194:10)
    at Interface._line (readline.js:523:8)
    at Interface._ttyWrite (readline.js:798:14)

@yleo77
Copy link
Author

yleo77 commented May 16, 2014

image

node 版本 v0.11.13

@ruanyf
Copy link
Owner

ruanyf commented May 16, 2014

谢谢指出,已经改过来了。

不好意思,上次没有仔细看源码,加上我的node是v0.11.11,确实会报错,就没试v0.11.13……

@yleo77
Copy link
Author

yleo77 commented May 19, 2014

😄

另外,从 nodejs 的不同版本来看,这里是有改动,但是从更新日志却没找到这部分说明。

@ruanyf
Copy link
Owner

ruanyf commented May 19, 2014

可能是v8改了……

On Mon, May 19, 2014 at 11:37 AM, yleo77 notifications@github.com wrote:

[image: 😄]

另外,从 nodejs 的不同版本来看,这里是有改动,但是从更新日志却没找到这部分说明。


Reply to this email directly or view it on GitHubhttps://github.com//issues/9#issuecomment-43463851
.

@ruanyf ruanyf closed this as completed Jan 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants