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

生成器函数第二次执行有问题 #784

Open
rimifon opened this issue Jul 6, 2024 · 1 comment
Open

生成器函数第二次执行有问题 #784

rimifon opened this issue Jul 6, 2024 · 1 comment

Comments

@rimifon
Copy link

rimifon commented Jul 6, 2024

function* test() {
	console.log("Started");
	yield sleep(1234);
	console.log("End");
	yield sleep(2345);
	console.log("============");
};
var gen = test();
function sleep(ms) { setTimeout(() => gen.next(), ms || 1); }
gen.next();
// 等待完成后,test 变成普通函数了,会立即执行,不需要 gen.next();
gen = test();

上述代码执行 gen.next() 后,结果是正确的。但如果再次执行 gen = test(); 结果就不对了,生成器函数成了普通函数,还没等 gen.next() 就立即执行了,而且 yield 等待也没生效。

下图是 fibjs(左)和 nodejs(右)的运行结果对比:
4992ecda7fb8009d14f3b5274f8f160

@xicilion
Copy link
Member

xicilion commented Jul 16, 2024

这段代码貌似是在 cli 中运行会不一致,我在脚本里运行结果是一样的。同时这段代码,在 0.37 和 0.38-dev cli 中的行为也不同。

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