-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Nesting generators compiles to incorrect JavaScript #17276
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
Comments
function* infiniteRepeater(gen) {
while (true) {
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const repeater = yield *gen();
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
for (const el of repeater) {
yield el;
}
}
}
function* yieldAB () {
yield 'a';
yield 'b';
}
const repeater = infiniteRepeater(yieldAB);
console.log("Logging first element of repeater");
console.log(repeater.next());
console.log("Element logged."); |
This is true (and thank you for the suggestion olegdunkan; the JS generated by this change and removing the "for const el of repeater" block below the !!!! lines works as expected instead of infinite looping). However, I believe this is still a bug; the code you recommended and the original should have the same result (as they do in JS), correct? |
Note: This workaround didn't actually serve our purpose; I left out a detail in the minimum example that we needed in practice in the production code. To guard against the possibility that gen yields no values (which would make olegdunkan's suggestion infinite loop), one needs to check the count of values yielded by gen() and if it's == 0, return from infiniteRepeater instead of building a new gen that again returns 0 results, etc. Both the minimum example at the top of this issue and the construction I've described show the same bad behavior of not allowing the polyfilled generator to be passed as an argument to a generator. |
@fixermark While generators are allowed in ES5, we do not change the semantics of
|
We chose not to always down-level |
TypeScript Version: version on typescriptlang.org/play/ on 2017-7-18 (assuming 2.4.0 / nightly (2.5.0-dev.201xxxxx)
Code
Expected behavior:
As seen when run in the browser's JavaScript interpreter, log output should be
Logging first element of repeater
Object {value: "A", done: false}
Element logged.
Actual behavior:
Infinite loop. It appears the generated JavaScript code (shown below) does not allow a generator to be passed as an argument to a generator.
Generated Code
Here is the output in the right-hand panel of http://typescriptlang.org/play/
The text was updated successfully, but these errors were encountered: